-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconcurent.php
More file actions
33 lines (24 loc) · 894 Bytes
/
concurent.php
File metadata and controls
33 lines (24 loc) · 894 Bytes
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
<?php
use Amp\Delayed;
use PHPinnacle\Ensign\DispatcherBuilder;
require __DIR__ . '/../vendor/autoload.php';
Amp\Loop::run(function () {
$builder = new DispatcherBuilder;
$builder
->register('emit', function (string $string, int $num, int $delay = 100) {
for ($i = 0; $i < $num; $i++) {
echo $string;
yield new Delayed($delay);
}
return $num;
})
;
$dispatcher = $builder->build();
$times = \rand(5, 10);
$actionOne = $dispatcher->dispatch('emit', '-', $times, 100);
$actionTwo = $dispatcher->dispatch('emit', '+', $times + \rand(5, 10), 100);
[$resultOne, $resultTwo] = yield [$actionOne, $actionTwo];
echo \PHP_EOL;
echo \sprintf('Action one done %d times.' . \PHP_EOL, $resultOne);
echo \sprintf('Action two done %d times.' . \PHP_EOL, $resultTwo);
});