PHP Fibers cooperative scheduler: minimal working example
Fibers without an event loop still have practical uses: cooperative multitasking in CLI scripts, lazy pipelines, and state machines.
Minimal round-robin scheduler in pure PHP, no extensions:
Output shows interleaved execution. Each task runs one step then suspends.
The Generator-based scheduler predates Fibers (PHP 5.5). With PHP 8.1 Fibers you get the same pattern but with a cleaner API: Fiber::suspend() instead of yield, and you can suspend from any depth in the call stack, not just from the generator function directly.
This pattern is what ReactPHP and Amp build on top of. The difference is they add an event loop that integrates with actual I/O readiness (socket reads, stream data) as the suspend conditions.
Useful for CLI progress display: one “task” reads data, another updates the terminal progress bar. No threading, no async, just cooperative.
The Fiber version is more flexible because suspend can cross function boundaries. With generators you cannot yield from inside a called function, only from the generator itself.
One practical use: chunked file processing with progress updates. The main loop processes data, a second “fiber” handles printing progress without blocking the data processing.
```php blocks are runnable.