1
0
mirror of https://github.com/danog/amp.git synced 2024-12-11 17:09:40 +01:00
amp/examples/pipeline/pipeline-source.php
2020-10-06 23:40:14 -05:00

41 lines
1.0 KiB
PHP

#!/usr/bin/env php
<?php
require __DIR__ . '/../../vendor/autoload.php';
use Amp\Loop;
use Amp\Pipeline;
use Amp\PipelineSource;
use function Amp\delay;
try {
/** @psalm-var PipelineSource<int> $source */
$source = new PipelineSource;
Loop::defer(function () use ($source): void {
// Source emits all values at once without awaiting back-pressure.
$source->emit(1);
$source->emit(2);
$source->emit(3);
$source->emit(4);
$source->emit(5);
$source->emit(6);
$source->emit(7);
$source->emit(8);
$source->emit(9);
$source->emit(10);
$source->complete();
});
$pipeline = $source->pipe();
// Use Amp\Pipeline\toIterator() to use a pipeline with foreach.
foreach (Pipeline\toIterator($pipeline) as $value) {
\printf("Pipeline source yielded %d\n", $value);
delay(100); // Listener consumption takes 100 ms.
}
} catch (\Throwable $exception) {
\printf("Exception: %s\n", (string) $exception);
}