2017-01-04 02:10:27 +01:00
|
|
|
#!/usr/bin/env php
|
|
|
|
<?php
|
|
|
|
|
2017-03-12 19:54:52 +01:00
|
|
|
require __DIR__ . '/../../vendor/autoload.php';
|
2017-01-04 02:10:27 +01:00
|
|
|
|
2017-03-10 21:31:57 +01:00
|
|
|
use Amp\Coroutine;
|
2017-04-23 14:43:46 +02:00
|
|
|
use Amp\Loop;
|
2017-03-10 21:31:57 +01:00
|
|
|
use Amp\Pause;
|
|
|
|
use Amp\Producer;
|
|
|
|
use Amp\Stream;
|
2017-04-23 14:43:46 +02:00
|
|
|
use Amp\StreamIterator;
|
2017-03-10 21:31:57 +01:00
|
|
|
|
|
|
|
Loop::run(function () {
|
2017-01-04 02:10:27 +01:00
|
|
|
try {
|
|
|
|
$producer = new Producer(function (callable $emit) {
|
|
|
|
yield $emit(1);
|
|
|
|
yield $emit(new Pause(500, 2));
|
|
|
|
yield $emit(3);
|
|
|
|
yield $emit(new Pause(300, 4));
|
|
|
|
yield $emit(5);
|
|
|
|
yield $emit(6);
|
|
|
|
yield $emit(new Pause(1000, 7));
|
|
|
|
yield $emit(8);
|
|
|
|
yield $emit(9);
|
|
|
|
yield $emit(new Pause(600, 10));
|
|
|
|
return 11;
|
|
|
|
});
|
|
|
|
|
|
|
|
$generator = function (Stream $stream) {
|
2017-04-13 18:20:46 +02:00
|
|
|
$listener = new StreamIterator($stream);
|
2017-01-04 02:10:27 +01:00
|
|
|
|
|
|
|
while (yield $listener->advance()) {
|
|
|
|
printf("Stream emitted %d\n", $listener->getCurrent());
|
|
|
|
yield new Pause(100); // Listener consumption takes 100 ms.
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("Stream result %d\n", $listener->getResult());
|
|
|
|
};
|
|
|
|
|
|
|
|
yield new Coroutine($generator($producer));
|
|
|
|
} catch (\Exception $exception) {
|
|
|
|
printf("Exception: %s\n", $exception);
|
|
|
|
}
|
2017-04-24 15:39:08 +02:00
|
|
|
});
|