callable = $callable; $this->name = $name; } /** * Main loop. * * @return \Generator */ public function loop(): \Generator { $callable = $this->callable; $number = 0; while (true) { $number = yield from $callable($number); echo "$this: $number".PHP_EOL; } } /** * Get loop name. * * @return string */ public function __toString(): string { return $this->name; } } AmpLoop::run(function () { $function = function (int $number): \Generator { yield delay(1000); return $number + 1; }; $loops = []; for ($x = 0; $x < 10; $x++) { $loop = new MyLoop($function, "Loop number $x"); $loop->start(); yield delay(100); $loops []= $loop; } });