1
0
mirror of https://github.com/danog/loop.git synced 2024-12-04 10:18:02 +01:00
loop/examples/1. Basic/PeriodicLoop.php

32 lines
697 B
PHP
Raw Normal View History

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