-
-
Notifications
You must be signed in to change notification settings - Fork 249
/
backpressure.php
40 lines (34 loc) 路 1.15 KB
/
backpressure.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
#!/usr/bin/env php
<?php
require __DIR__ . '/../../vendor/autoload.php';
use Amp\Coroutine;
use Amp\Emitter;
use Amp\Loop;
use Amp\Pause;
use Amp\Promise;
Loop::run(function () {
try {
$emitter = new Emitter;
$iterator = $emitter->getIterator();
$generator = function (Emitter $emitter) {
yield $emitter->emit(new Pause(500, 1));
yield $emitter->emit(new Pause(1500, 2));
yield $emitter->emit(new Pause(1000, 3));
yield $emitter->emit(new Pause(2000, 4));
yield $emitter->emit(5);
yield $emitter->emit(6);
yield $emitter->emit(7);
yield $emitter->emit(new Pause(2000, 8));
yield $emitter->emit(9);
yield $emitter->emit(10);
$emitter->complete();
};
Promise\rethrow(new Coroutine($generator($emitter)));
while (yield $iterator->advance()) {
printf("Stream emitted %d\n", $iterator->getCurrent());
yield new Pause(500); // Listener consumption takes 500 ms.
}
} catch (\Exception $exception) {
printf("Exception: %s\n", $exception);
}
});