dmitry_kv30 Dec 2025 18:42

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:

PHP
<?php
class Scheduler
{
private array $tasks = [];
public function add(Generator $task): void
{
$this->tasks[] = $task;
}
public function run(): void
{
while (!empty($this->tasks)) {
foreach ($this->tasks as $key => $task) {
$task->current(); // run until next yield
if (!$task->valid()) {
unset($this->tasks[$key]);
} else {
$task->next();
}
}
}
}
}
function counter(string $name, int $from, int $to): Generator
{
for ($i = $from; $i <= $to; $i++) {
echo "{$name}: {$i}\n";
yield; // cooperative suspend point
}
}
$s = new Scheduler();
$s->add(counter('A', 1, 4));
$s->add(counter('B', 1, 4));
$s->add(counter('C', 1, 4));
$s->run();
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Output shows interleaved execution. Each task runs one step then suspends.

Replies (5)
alex_petrov30 Dec 2025 19:00

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.

0
vova30 Dec 2025 19:30

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.

0
marcoviola30 Dec 2025 21:04

Useful for CLI progress display: one “task” reads data, another updates the terminal progress bar. No threading, no async, just cooperative.

0
dmitry_kv30 Dec 2025 22:08

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.

0
jnovak30 Dec 2025 23:46

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.

0
Write a reply
Markdown. ```php blocks are runnable.