Fibers¶
Fibers in PHP are a low-level mechanism for lightweight concurrency, they allow to pause and resume blocks of code at specific points without blocking the rest of the application.
They were introduced in PHP 8.1 and provide cooperative multitasking, as opposed to preemptive multitasking like threads. This means the application controls exactly where execution yields and resumes.
<?php
$fiber = new Fiber(function (): void {
$value = Fiber::suspend('fiber');
echo "Value used to resume fiber:", $value, PHP_EOL;
});
$value = $fiber->start();
echo "Value from fiber suspending:", $value, PHP_EOL;
$fiber->resume('test');
?>
See also PHP Fibers: A practical example, Async PHP is Here: A Practical Guide to Fibers, Fibers in PHP - how to make it easier to implement asynchronicity in your project, Unleashing the Power of PHP Fibers: Boost Web Development with Efficient Coroutines, PHP Fibers in Production: 4 Real Cases Where They Beat curl_multi and Queues and PHP Fibers: simplifying async code and speeding up development.
Related : Async, Asynchronous, Multithreading, Blocking Wait, Cancellation, Future, Promise
Related packages : amphp/process
Added in PHP 5.0+