How do Fibers in PHP 8.1 facilitate cooperative multitasking, and how do they differ from true multi-threading?
Last updated
On a blocking wait (e.g. network I/O) a Fiber calls
Fiber::suspend()instead of blocking the process. An event loop runs other Fibers and resumes it when the I/O is ready.Unlike OS threads (preemptive, parallel across cores), Fibers run one at a time and switch only when code yields. So no data races, but a CPU-heavy Fiber that never suspends starves the others.
Fibers solve I/O-bound concurrency. For parallel CPU work you still need processes or extensions like
parallel.
- https://en.wikipedia.org/wiki/PHP
- https://lemon.io/for-developers/php-developer-jobs/
- https://lan-portal.uob.edu.ly/visit/BOOK/529X432U02/modern-php_new__features_and_good-practices.pdf
- https://lan-portal.uob.edu.ly/upload/MD/D57953S372/modern-php__new_features_and__good__practices.pdf
- https://news.ycombinator.com/item?id=30786927
- https://kvnc-inc.medium.com/solid-principles-in-php-1c41069e9af6
- https://www.shkodenko.com/
- https://confoo.ca/en/2024/sessions