-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsleep.php
More file actions
50 lines (36 loc) · 1.35 KB
/
sleep.php
File metadata and controls
50 lines (36 loc) · 1.35 KB
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
48
49
50
<?php
declare(strict_types=1);
use React\EventLoop\Loop;
use ReactParallel\EventLoop\EventLoopBridge;
use ReactParallel\Pool\Infinite\Infinite;
use function React\Async\async;
use function React\Async\await;
use function React\Promise\all;
use function WyriHaximus\iteratorOrArrayToArray;
require dirname(__DIR__) . DIRECTORY_SEPARATOR . 'vendor' . DIRECTORY_SEPARATOR . 'autoload.php';
$infinite = new Infinite(new EventLoopBridge(), 0.1);
Loop::futureTick(async(static function () use ($infinite): void {
$timer = Loop::addPeriodicTimer(1, static function () use ($infinite): void {
var_export(iteratorOrArrayToArray($infinite->info()));
});
$promises = [];
foreach (range(0, 250) as $i) {
$promises[] = async(static function (Infinite $infinite, int $i): int {
$sleep = $infinite->run(static function (int $sleep): int {
sleep($sleep);
return $sleep;
}, [random_int(1, 13)]);
echo $i, '; ', $sleep, PHP_EOL;
return $sleep;
})($infinite, $i);
}
$signalHandler = static function () use ($infinite): void {
$infinite->close();
Loop::stop();
};
Loop::addSignal(SIGINT, $signalHandler);
await(all($promises));
$infinite->close();
Loop::removeSignal(SIGINT, $signalHandler);
Loop::cancelTimer($timer);
}));