1
0
mirror of https://github.com/danog/loop.git synced 2025-01-22 13:21:22 +01:00
loop/examples/1. Basic/GenericLoop.php

32 lines
712 B
PHP
Raw Normal View History

2022-12-24 14:36:39 +01:00
<?php declare(strict_types=1);
2020-07-23 14:18:33 +02:00
require 'vendor/autoload.php';
use danog\Loop\Generic\GenericLoop;
use function Amp\delay;
2022-12-24 14:36:39 +01:00
/** @var GenericLoop[] */
$loops = [];
for ($x = 0; $x < 10; $x++) {
$callable = function () {
static $number = 0;
echo "$this: $number".PHP_EOL;
$number++;
return $number < 10 ? 1000 : GenericLoop::STOP;
};
$loop = new GenericLoop($callable, "Loop number $x");
$loop->start();
delay(100);
$loops []= $loop;
}
delay(5000);
echo "Resuming prematurely all loops!".PHP_EOL;
foreach ($loops as $loop) {
$loop->resume();
}
echo "OK done, waiting 5 more seconds!".PHP_EOL;
delay(5000);
echo "Closing all loops!".PHP_EOL;
delay(10);