-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathLoop.php
47 lines (39 loc) · 914 Bytes
/
Loop.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php declare(strict_types=1);
require 'vendor/autoload.php';
use danog\Loop\GenericLoop;
use danog\Loop\Loop;
use function Amp\delay;
class MyLoop extends Loop
{
private int $number = 0;
public function __construct(private string $name)
{
}
protected function loop(): ?float
{
echo "$this: {$this->number}".PHP_EOL;
$this->number++;
return $this->number < 10 ? 1.0 : GenericLoop::STOP;
}
public function __toString(): string
{
return $this->name;
}
}
/** @var MyLoop[] */
$loops = [];
for ($x = 0; $x < 10; $x++) {
$loop = new MyLoop("Loop number $x");
$loop->start();
delay(0.1);
$loops []= $loop;
}
delay(5);
echo "Resuming prematurely all loops!".PHP_EOL;
foreach ($loops as $loop) {
$loop->resume();
}
echo "OK done, waiting 5 more seconds!".PHP_EOL;
delay(5);
echo "Closing all loops!".PHP_EOL;
delay(0.01);