generators for asynchronous...
TRANSCRIPT
![Page 1: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/1.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/2.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/3.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/4.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/5.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/6.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/7.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/8.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/9.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/10.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/11.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/12.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/13.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/14.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/15.jpg)
⚠ 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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/16.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/17.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/18.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/19.jpg)
☕ 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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/20.jpg)
🗣 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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/21.jpg)
🌬 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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/22.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/23.jpg)
😋🌡⏱ 👂 ⏱ ⏱ 👂 ⏱
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/24.jpg)
🌬🌬🗣 🌬 🗣 ☕ 🌬 🗣 😋🌡⏱ 👂 ⏱ ⏱ 👂 ⏱
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/25.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/26.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/27.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/28.jpg)
▶🌬
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/29.jpg)
🌬
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/30.jpg)
▶🗣
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/31.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/32.jpg)
▶
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/33.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/34.jpg)
▶⏸
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/35.jpg)
⏸🆗🌬⏱
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/36.jpg)
🆗 🌬🌬⏱
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/37.jpg)
▶⏸
🌬🌬⏱
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/38.jpg)
▶⏸
🆗
🌬🌬⏱
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/39.jpg)
⏸🆗🌡
🌬🌬⏱
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/40.jpg)
🆗 ☕🌡
🌬🌬⏱
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/41.jpg)
▶⏸
☕🌡
🌬🌬⏱
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/42.jpg)
⏸🆗▶ ⏱🌬🌬⏱
☕🌡
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/43.jpg)
▶⏸
☕🌡
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/44.jpg)
⏸🗣👂
☕🌡
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/45.jpg)
🗣🗣 👂
☕🌡
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/46.jpg)
▶⏸ 🌬⏱🌬🌬⏱
😋☕🌡
🗣🗣 👂
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/47.jpg)
⏸🆗▶ ⏱🌬⏱🌬🌬⏱ ⏱🌬
😋☕🌡
🗣🗣 👂
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/48.jpg)
▶⏸🆗
🌬⏱🌬⏱🌬🌬⏱
😋☕🌡
🗣🗣 👂
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/49.jpg)
⏸ 👂
🌬⏱
😋☕🌡
🗣🗣 👂
🌬⏱🌬🌬⏱
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/50.jpg)
▶⏸
🗣👂
🌬⏱
😋☕🌡
🗣🗣 👂
🌬⏱🌬🌬⏱
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/51.jpg)
⏱
🗣👂
🌬⏱
😋☕🌡
🗣🗣 👂
🌬⏱🌬🌬⏱
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/52.jpg)
⏱
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/53.jpg)
⏱
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/54.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/55.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/56.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/57.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/58.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/59.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/60.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/61.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/62.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/63.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/64.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/65.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/66.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/67.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/68.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/69.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/70.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/71.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/72.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/73.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/74.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/75.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/76.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/77.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/78.jpg)
👇
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/79.jpg)
👇
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/80.jpg)
☝
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/81.jpg)
☝
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/82.jpg)
👇
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/83.jpg)
👇
!$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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/84.jpg)
👇
$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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/85.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/86.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/87.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/88.jpg)
Promises🎁
![Page 89: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/89.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/90.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/91.jpg)
🗣 🗣👂 🗣 👂
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/92.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/93.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/94.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/95.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/96.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/97.jpg)
🔥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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/98.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/99.jpg)
🗣 🗣 🗣👂👂
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/100.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/101.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/102.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/103.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/104.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/105.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/106.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/107.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/108.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/109.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/110.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/111.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/112.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/113.jpg)
🗣 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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/114.jpg)
🌬 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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/115.jpg)
☕ 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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/116.jpg)
☕ 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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/117.jpg)
☕ 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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/118.jpg)
☕ 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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/119.jpg)
☕ 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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/120.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/121.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/122.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/123.jpg)
So…
![Page 124: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/124.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/125.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/126.jpg)
Threads
![Page 127: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/127.jpg)
![Page 128: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/128.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/129.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/130.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/131.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/132.jpg)
PRACTICE
![Page 133: Generators for Asynchronous Programmingb-viguier.github.io/downloads/talks/ForumPhp-AsyncGenerators.pdf · Only yield matters function emptyGenerator(): \Generator return 1; yield;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/133.jpg)
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;](https://reader031.vdocuments.net/reader031/viewer/2022011900/5ee15f0dad6a402d666c470e/html5/thumbnails/134.jpg)
Thanks !
Benoit Viguier@b_viguier
$answer = yield $you->ask($me);