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-04-28 14:42:02 +02:00
|
|
|
use Amp\Delayed;
|
2017-04-23 14:43:46 +02:00
|
|
|
use Amp\Loop;
|
2017-03-10 21:31:57 +01:00
|
|
|
use Amp\Producer;
|
|
|
|
|
|
|
|
Loop::run(function () {
|
2017-01-04 02:10:27 +01:00
|
|
|
try {
|
2017-04-27 17:51:06 +02:00
|
|
|
$iterator = new Producer(function (callable $emit) {
|
2017-01-04 02:10:27 +01:00
|
|
|
yield $emit(1);
|
2017-04-28 14:42:02 +02:00
|
|
|
yield $emit(new Delayed(500, 2));
|
2017-01-04 02:10:27 +01:00
|
|
|
yield $emit(3);
|
2017-04-28 14:42:02 +02:00
|
|
|
yield $emit(new Delayed(300, 4));
|
2017-01-04 02:10:27 +01:00
|
|
|
yield $emit(5);
|
|
|
|
yield $emit(6);
|
2017-04-28 14:42:02 +02:00
|
|
|
yield $emit(new Delayed(1000, 7));
|
2017-01-04 02:10:27 +01:00
|
|
|
yield $emit(8);
|
|
|
|
yield $emit(9);
|
2017-04-28 14:42:02 +02:00
|
|
|
yield $emit(new Delayed(600, 10));
|
2017-01-04 02:10:27 +01:00
|
|
|
return 11;
|
|
|
|
});
|
|
|
|
|
2017-04-27 17:51:06 +02:00
|
|
|
while (yield $iterator->advance()) {
|
2017-11-29 13:36:50 +01:00
|
|
|
\printf("Producer emitted %d\n", $iterator->getCurrent());
|
2017-04-26 19:20:30 +02:00
|
|
|
yield new Delayed(100); // Listener consumption takes 100 ms.
|
|
|
|
}
|
2017-01-04 02:10:27 +01:00
|
|
|
} catch (\Exception $exception) {
|
2017-11-29 13:36:50 +01:00
|
|
|
\printf("Exception: %s\n", $exception);
|
2017-01-04 02:10:27 +01:00
|
|
|
}
|
2017-04-24 15:39:08 +02:00
|
|
|
});
|