Generators for Asynchronous...

134
Generators for Asynchronous Programming User Manual Benoit Viguier @b_viguier

Transcript of Generators for Asynchronous...

Page 1: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Generators for Asynchronous Programming

User Manual

Benoit Viguier@b_viguier

Page 2: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Generators for Asynchronous Programming

User Manual

Benoit Viguier@b_viguier

Page 3: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Generators for Asynchronous Programming

User Manual

Benoit Viguier@b_viguier

Page 4: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Generators for Asynchronous Programming

User Manual

Benoit Viguier@b_viguier

Page 5: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Generators for Asynchronous Programming

User Manual

Benoit Viguier@b_viguier

Page 6: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Generators for Asynchronous Programming

User Manual

Benoit Viguier@b_viguier

Page 7: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Asynchronous Programming

Page 8: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

PHP DEV

Y U NO USE JAVASCRIPT

Page 9: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

PHP DEV

Y U NO USE PHP

Page 10: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Tea for TwoAn illustrated example

Page 11: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Tasks

•☕ Drinking Tea

•🗣 Discussing with a friend

•🌬 Breathing just… to live

Page 12: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Synchronous

🌬 Breath ☕ Drink🗣 Discuss

Time

Page 13: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

ConcurrencyStarting next task before that current one ends

Page 14: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Parallel

🌬 Breath

☕ Drink

🗣 Discuss

Time

Page 15: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

⚠ Php is single threaded ⚠… and it’s a good thing

Page 16: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Asynchronous

Time

☕🌬 ☕🗣 🌬 🌬 🗣 🌬 🗣 🌬 ☕ 🌬 🗣 🌬 🗣 🌬

Page 17: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

But… 🤔

Time

🌬 Breath ☕ Drink🗣 Discuss

☕🌬 ☕🗣 🌬 🌬 🗣 🌬 🗣 🌬 ☕ 🌬 🗣 🌬 🗣 🌬

Page 18: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Analyzing tasksInternal/External operations

Page 19: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

☕ Drinking Tea

🌡 Waiting good temperature

☕ Drinking (actually)

😋 Enjoying🗣😋🌡 ☕

Page 20: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

🗣 Discussing with a friend

🗣 Speaking, asking something

👂 Listening

🗣🗣 👂 🗣 👂

Page 21: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

🌬 Breathing

⏱🌬⏱ 🌬 ⏱ 🌬 ⏱ 🌬

🌬 Breathing…

⏱ Waiting (a little)

🌬 Breathing…

Page 22: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Asynchronous

Time

😋🌬 🌡🗣 ⏱ 🌬 👂 ⏱ 🗣 🌬 ☕ ⏱ 👂 🌬 🗣 ⏱

Page 23: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

😋🌡⏱ 👂 ⏱ ⏱ 👂 ⏱

Internal operations

Time

🌬🗣 🌬 🗣 🌬 ☕ 🌬 🗣

Page 24: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

🌬🌬🗣 🌬 🗣 ☕ 🌬 🗣 😋🌡⏱ 👂 ⏱ ⏱ 👂 ⏱

External operations

Time

Page 25: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Dispatching executionEvent Loop

Page 26: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Event Loop

🗣 👂 🗣 👂

🌬⏱ 🌬 ⏱ 🌬 ⏱ 🌬 ⏱

🗣

🌡 😋

Page 27: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Event Loop

🗣 👂 🗣 👂

🌬⏱ 🌬 ⏱ 🌬 ⏱ 🌬👉▶ ⏱

🗣

🌡 😋

Page 28: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

▶🌬

Event Loop

🗣 👂 🗣 👂

⏱ 🌬 ⏱ 🌬 ⏱ 🌬⏸👉 ⏱

🗣

🌡 😋

Page 29: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

🌬

Event Loop

🗣 👂 🗣 👂

⏱ 🌬 ⏱ 🌬 ⏱ 🌬

👉▶

🗣

🌡 😋

▶⏸

Page 30: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

▶🗣

Event Loop

👂 🗣 👂👉

🌬⏱ 🌬 ⏱ 🌬 ⏱ 🌬

🗣

🌡 😋

▶⏸

Page 31: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Event Loop

☕👉

🗣👂 🗣 👂

🌬⏱ 🌬 ⏱ 🌬 ⏱ 🌬

🗣

🌡 😋

▶⏸

▶⏸

Page 32: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Event Loop

☕👉

🗣👂 🗣 👂

🌬⏱ 🌬 ⏱ 🌬 ⏱ 🌬

🗣

🌡 😋

▶⏸

▶⏸

Page 33: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Event Loop

} Tick

👉 ☕

🗣👂 🗣 👂

🌬⏱ 🌬 ⏱ 🌬 ⏱ 🌬 ⏱

🗣

🌡 😋

▶⏸

▶⏸

▶⏸

Page 34: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

▶⏸

Event Loop

👉

🗣👂 🗣 👂

🌬⏱ 🌬 ⏱ 🌬 ⏱ 🌬🆗 ⏱

🗣

🌡 😋

▶⏸

▶⏸

Page 35: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

⏸🆗🌬⏱

Event Loop

👉

🗣👂 🗣 👂

🌬 ⏱ 🌬 ⏱ 🌬 ⏱

🗣

🌡 😋

▶⏸

▶⏸

Page 36: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

🆗 🌬🌬⏱

Event Loop

👉

🗣👂 🗣 👂

⏱ 🌬 ⏱ 🌬 ⏱

🗣

🌡 😋

▶⏸

▶⏸

▶⏸

Page 37: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

▶⏸

🌬🌬⏱

Event Loop

🗣 👂 🗣 👂

⏱ 🌬 ⏱ 🌬

👉🆗❌

🗣

🌡 😋

🆗▶⏸

▶⏸

Page 38: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

▶⏸

🆗

🌬🌬⏱

Event Loop

🗣 👂 🗣 👂

⏱ 🌬 ⏱ 🌬

🆗👉

🗣

🌡 😋

🆗▶⏸

▶⏸

Page 39: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

⏸🆗🌡

🌬🌬⏱

Event Loop

🗣 👂 🗣 👂

⏱ 🌬 ⏱ 🌬

👉 😋☕

🗣

🆗▶⏸

🆗▶⏸

Page 40: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

🆗 ☕🌡

🌬🌬⏱

Event Loop

🗣 👂 🗣 👂

⏱ 🌬 ⏱ 🌬

👉 😋

🗣

🆗▶⏸

🆗▶⏸

▶⏸

Page 41: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

▶⏸

☕🌡

🌬🌬⏱

Event Loop

🗣 👂 🗣 👂

⏱ 🌬 ⏱ 🌬

😋

👉 } Tick

🗣

🆗

🆗▶⏸

🆗▶⏸

Page 42: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

⏸🆗▶ ⏱🌬🌬⏱

☕🌡

Event Loop

🗣 👂 🗣 👂

😋

👉 🌬 ⏱🌬⏱

🗣🆗▶⏸

🆗▶⏸

Page 43: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

▶⏸

☕🌡

Event Loop

🗣 👂 🗣 👂

😋

👉

🌬⏱🌬🌬⏱ ⏱🌬⏱

🗣

🆗▶⏸

🆗

🆗▶⏸

Page 44: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

⏸🗣👂

☕🌡

Event Loop

🗣 👂

😋

👉

🌬⏱🌬🌬⏱ ⏱🌬⏱

🗣

🆗▶⏸

🆗▶

🆗▶⏸

Page 45: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

🗣🗣 👂

☕🌡

Event Loop

👂

😋👉

🌬⏱🌬🌬⏱ ⏱🌬⏱

🗣

🆗▶⏸

🆗▶⏸

🆗

Page 46: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

▶⏸ 🌬⏱🌬🌬⏱

😋☕🌡

🗣🗣 👂

Event Loop

👂

👉 } Tick⏱🌬⏱

🗣

🆗

🆗▶⏸

Page 47: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

⏸🆗▶ ⏱🌬⏱🌬🌬⏱ ⏱🌬

😋☕🌡

🗣🗣 👂

Event Loop

👂

👉

🗣🆗▶⏸

Page 48: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

▶⏸🆗

🌬⏱🌬⏱🌬🌬⏱

😋☕🌡

🗣🗣 👂

Event Loop

👂👉

🗣

🆗▶⏸

Page 49: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

⏸ 👂

🌬⏱

😋☕🌡

🗣🗣 👂

🌬⏱🌬🌬⏱

Event Loop

🗣

👉

🆗▶⏸

🆗▶

Page 50: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

▶⏸

🗣👂

🌬⏱

😋☕🌡

🗣🗣 👂

🌬⏱🌬🌬⏱

Event Loop

⏱👉🆗

Page 51: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

🗣👂

🌬⏱

😋☕🌡

🗣🗣 👂

🌬⏱🌬🌬⏱

Event Loop

Page 52: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Asynchronous

Time

😋⏱ 🌡 ⏱

👂 ⏱👂

🗣🌬 🗣 🗣 🌬🌬 ☕ 🌬

Page 53: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Asynchronous

Time

😋⏱ 🌡 ⏱

👂 ⏱👂

🗣🌬 🗣 🗣 🌬🌬 ☕ 🌬Php

Not Php

Page 54: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

How to dispatch code execution efficiently?

🤔

Page 55: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

GeneratorsSubset of coroutines

Page 56: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Examplefunction gen_one_to_three() { for ($i = 1; $i <= 3; $i++) { yield $i; } }

$generator = gen_one_to_three(); foreach ($generator as $value) { echo "$value\n"; }

Page 57: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Starting with Generators… by the weird part…

Page 58: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Creation// ❌ Does NOT work $generator = new \Generator;

// PHP Fatal error: Uncaught Error: // The "Generator" class is reserved // for internal use // and cannot be manually instantiated

Page 59: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Creationfunction create(): \Generator { yield; }

$generator1 = create(); $generator2 = create();

Page 60: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Only yield mattersfunction emptyGenerator(): \Generator { return 1; yield; // Never reached… }

$generator = emptyGenerator();

Page 61: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Execution startfunction dyingGenerator(): \Generator { die('hard'); yield; } // The function is not executed… $generator = dyingGenerator(); // Will die ‘hard’ $generator->valid();

Page 62: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

final class Generator implements Iterator {

function rewind() {} function valid(): bool {} function current() {} function key() {} function next() {}

function send($value) {} function throw(Throwable $exception) {} function getReturn() {} }

Page 63: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Out

side

Inside

$generator = createMyGenerator(); // Here, outside content

function createMyGenerator(): \Generator { // Here, inside content }

Page 64: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Out

side

Inside

$value = $generator->current(); $key = $generator->key();

yield $key => $value;

Page 65: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Out

side

Inside

$generator->send($value); $generator->next();

$value = yield; $nullValue = yield;

Page 66: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Out

side

Inside

$v1 = $generator->current(); $v3 = $generator->send($v2);

$v2 = yield $v1; yield $v3;

Page 67: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Outside

Inside

Page 68: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Outside

Inside

👇

$generator = createMyGenerator()

Page 69: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Outside

Inside

👇

$v1 = $generator->current()

Page 70: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Outside

Inside

$v1 = $generator->current()

Page 71: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Outside

Inside

$v2 = yield $v1

$v1 = $generator->current()

Page 72: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Outside

Inside

👇

$v2 = yield $v1

$v1 = $generator->current()

Page 73: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Inside

👇

$v3 = $generator->send($v2)

$v2 = yield $v1

Page 74: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Inside

$v3 = $generator->send($v2)

$v2 = yield $v1

Page 75: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Inside

yield $v3

$v3 = $generator->send($v2)

Page 76: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Inside

👇

yield $v3

$v3 = $generator->send($v2)

Page 77: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Out

side

Inside

if (!$generator->valid()) { $value = $generator->getReturn(); }

return $value;

Page 78: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

👇

yield $v3

$v3 = $generator->send($v2)

Page 79: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

👇

yield $v3

$generator->next()

Page 80: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

yield $v3

$generator->next()

Page 81: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

return $value;

$generator->next()

Page 82: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

👇

return $value;

$generator->next()

Page 83: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

👇

!$generator->valid()

return $value;

Page 84: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

👇

$value = $generator->getReturn()

return $value;

Page 85: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Out

side

Inside

try { $generator->next(); } catch (\Exception $exception) {}

yield; throw new \Exception();

Page 86: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Out

side

Inside

$generator->throw($exception);

try { yield; } catch (\Exception $exception) {}

Page 87: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Generators for Asynchronous Programming

Let the magic happen

Page 88: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Promises🎁

Page 89: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Synchronousfunction discuss(string $question1, string $question2) { /** @var string $response */ $response = askQuestion($question1); echo $response;

/** @var string $response */ $response = askQuestion($question2); echo $response; }

Page 90: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

function askQuestion(string $question): string { // … }

Synchronous

Page 91: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

🗣 🗣👂 🗣 👂

Waiting for a result…

Here your result!

Promise

Page 92: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Asynchronous

function askQuestion(string $question): Promise { // … }

Page 93: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Asynchronousfunction discuss(string $question1, string $question2) { /** @var Promise $promise */ $promise = askQuestion($question1); // ⁉ echo $response;

/** @var Promise $promise */ $promise = askQuestion($question2); // ⁉ echo $response; }

Page 94: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Promises A+Thenable Promises

Page 95: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Event Loop

🗣 🗣🗣 👂 👂

then then

Page 96: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

function discuss(string $question1, string $question2) { $promise = askQuestion($question1); $promise->then( function(string $response) use($question2) { echo $response;

return askQuestion($question2); } )->then( function(string $response) { echo $response; } ); } 😕

Page 97: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

🔥Callback Hell🔥$promise ->then(function ($x) { return $x + 1; }) ->then(function ($x) { throw new \Exception($x + 1); }) ->otherwise(function (\Exception $x) { return $x->getMessage() + 1; }) ->then(function ($x) { echo 'Mixed '.$x; });

Page 98: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Generator Promises

Page 99: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

🗣 🗣 🗣👂👂

Event Loop

yield yield

Page 100: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Synchronousfunction discuss(string $question1, string $question2) {

/** @var string $response */ $response = askQuestion($question1); echo $response;

/** @var string $response */ $response = askQuestion($question2); echo $response; }

Page 101: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Asynchronousfunction discuss(string $question1, string $question2) {

/** @var string $response */ $response = yield askQuestion($question1); echo $response;

/** @var string $response */ $response = yield askQuestion($question2); echo $response; }

Page 102: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Asynchronousfunction discuss(string $question1, string $question2) { $promise = askQuestion($question1); /** @var string $response */ $response = yield $promise; echo $response;

$promise = askQuestion($question2); /** @var string $response */ $response = yield $promise; echo $response; }

Page 103: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

To asynchronously wait the value of a promise,

use yield.

Page 104: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Asynchronous Function: a generator

yielding only promises.

Page 105: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Getting started!

Page 106: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Finding the good use case

Page 107: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

A B C D E❌

A B D G J

C E H K

F I L

Page 108: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Choose your Event Loop

Page 109: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

reactphp/react

👍 Community

😩 Thenable Promises

Page 110: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

amphp/amp

👍 Generators

🤔 Coupling ?

Page 111: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

m6web/tornado

👍 Generators

😘 Interfaces / Adapters

🤔 M6Web

Page 112: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Tea for TwoLet’s code it!

Page 113: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

🗣 Discussing with a friendfunction discuss(string $question1, string $question2) : \Generator { /** @var string $response */ $response = yield askQuestion($question1); echo $response;

/** @var string $response */ $response = yield askQuestion($question2); echo $response; }

Page 114: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

🌬 Breathing

function breath(EventLoop $eventLoop, int $count) : \Generator { while (--$count >= 0) { echo "Breathing\n"; yield $eventLoop->idle(); } }

Page 115: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

☕ Drinking Teafunction tMax(EventLoop $eventLoop, int $maximum) : \Generator { do { yield $eventLoop->idle(); $current = rand(20, 40); } while ($current > $maximum);

return $current; }

Page 116: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

☕ Drinking Teafunction drink(EventLoop $eventLoop): \Generator { $generator = tMax($eventLoop, 35); $promise = $eventLoop->async($generator); $temperature = yield $promise;

echo "Drinking [$temperature °]\n";

yield $eventLoop->delay(1000/*ms*/); }

Page 117: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

☕ Drinking Teafunction drink(EventLoop $eventLoop): \Generator { $generator = tMax($eventLoop, 35); $promise = $eventLoop->async($generator); $temperature = yield $promise;

echo "Drinking [$temperature °]\n";

yield $eventLoop->delay(1000/*ms*/); }

Page 118: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

☕ Drinking Teafunction drink(EventLoop $eventLoop): \Generator { $generator = tMax($eventLoop, 35); $promise = $eventLoop->async($generator); $temperature = yield $promise;

echo "Drinking [$temperature °]\n";

yield $eventLoop->delay(1000/*ms*/); }

Page 119: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

☕ Drinking Teafunction drink(EventLoop $eventLoop): \Generator { $generator = tMax($eventLoop, 35); $promise = $eventLoop->async($generator); $temperature = yield $promise;

echo "Drinking [$temperature °]\n";

yield $eventLoop->delay(1000/*ms*/); }

Page 120: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Wait for it…$concurrentPromise = $eventLoop->promiseAll(

$eventLoop->async( breath($eventLoop, 10) ), $eventLoop->async( discuss('Where?', ‘What?') ), $eventLoop->async( drink($eventLoop) ) );

$eventLoop->wait($concurrentPromise);

Page 121: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Wait for it…$concurrentPromise = $eventLoop->promiseAll(

$eventLoop->async( breath($eventLoop, 10) ), $eventLoop->async( discuss('Where?', ‘What?') ), $eventLoop->async( drink($eventLoop) ) );

$eventLoop->wait($concurrentPromise);

Page 122: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Wait for it…$concurrentPromise = $eventLoop->promiseAll(

$eventLoop->async( breath($eventLoop, 10) ), $eventLoop->async( discuss('Where?', ‘What?') ), $eventLoop->async( drink($eventLoop) ) );

$eventLoop->wait($concurrentPromise);

Page 123: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

So…

Page 124: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Asynchronous Programmingdoesn’t require…

Page 125: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Http Server

Page 126: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Threads

Page 127: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;
Page 128: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Asynchronous Programmingrequires…

Page 129: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Event Loop

Page 130: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Interruptible Tasks

Page 131: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Generators:Powerful tool for

Asynchronous Programming

Page 132: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

PRACTICE

Page 133: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Generators for Asynchronous Programming

User Manual

Benoit Viguier@b_viguier

Page 134: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;

Thanks !

Benoit Viguier@b_viguier

$answer = yield $you->ask($me);