From Generator to Fiber The Road to Coroutine in PHP 02 Blocking and Async I/O in PHP Outline 01 I/O Models in Linux 03 What is Generator? 04 What is Fiber in PHP 8.1? 05 How to use Async I/O with Coroutine? 06 Coroutines in Swoole Read/Write Read/Write (O_NONBLOCK) I/O Multiplexing (select/poll/epoll) AIO I/O Models in Linux Simplified Matrix of Linux I/O Models Blocking Non-Blocking Synchronous Asynchronous (https://developer.ibm.com/articles/l-async) I/O Models in Linux Synchronous Blocking I/O (https://developer.ibm.com/articles/l-async) I/O Models in Linux Synchronous Non-Blocking I/O (https://developer.ibm.com/articles/l-async) I/O Models in Linux Asynchronous Blocking I/O (https://developer.ibm.com/articles/l-async) I/O Models in Linux Asynchronous Non-Blocking I/O (https://developer.ibm.com/articles/l-async) I/O Models in Linux Comparison of the I/O Models (https://rickhw.github.io/2019/02/27/ComputerScience/IO-Models) select poll epoll Data Structue Array Link List Hash Table Big O O(n) O(n) O(1) FD Limit 1024 (x86) 2048 (x64) Unlimited Unlimited I/O Multiplexing I/O Multiplexing in Linux I/O Multiplexing I/O Multiplexing in Linux (http://daemonforums.org/showthread.php?t=2124) Blocking I/O in PHP All I/O functions in PHP are born to be synchronous Easy for developers to understand Less-efficient for utilization of CPU usage sleep file_get_contents shell_exec end Blocking I/O in PHP 1s 3s 3s process idle + + process idle process idle 7s = 1s worker 1s worker 1s worker Blocking I/O in PHP If one request takes 1s in I/O waiting Each worker can handle only one request and the same time Concurrency in PHP depends on worker numbers Context switch between processes are expensive! request request request Asynchronous I/O in PHP The simplest but worst-performant solution in PHP spatie/async package (needs pcntl and posix extensions) Asynchronous I/O in PHP The simplest but worst-performant solution in PHP spatie/async package (needs pcntl and posix extensions) worker process process fork fork Asynchronous I/O in PHP ReactPHP Asynchronous I/O in PHP ReactPHP Asynchronous I/O in PHP Amp Asynchronous I/O in PHP Amp Asynchronous I/O in PHP Async I/O Extensions in PHP ext-event : libevent wrapper ext-ev : libev wrapper ext-uv : libuv wrapper Both ReactPHP and Amp support these extensions as event-loop drivers