1
0
mirror of https://github.com/danog/amp.git synced 2024-12-12 09:29:45 +01:00
amp/examples/streams/backpressure.php
2017-04-24 15:27:04 +02:00

49 lines
1.4 KiB
PHP

#!/usr/bin/env php
<?php
require __DIR__ . '/../../vendor/autoload.php';
use Amp\Coroutine;
use Amp\Emitter;
use Amp\Loop;
use Amp\Pause;
Loop::run(function () {
try {
$emitter = new Emitter;
$stream = $emitter->stream();
$stream->onEmit(function ($value) {
printf("Stream emitted %d\n", $value);
return new Pause(500); // Artificial back-pressure on stream.
});
$stream->onResolve(function (Throwable $exception = null, $value) {
if ($exception) {
printf("Stream failed: %s\n", $exception->getMessage());
return;
}
printf("Stream result %d\n", $value);
});
$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->resolve(11);
};
yield new Coroutine($generator($emitter));
} catch (\Exception $exception) {
printf("Exception: %s\n", $exception);
}
});