mirror of
https://github.com/danog/parallel.git
synced 2024-11-29 20:29:00 +01:00
Update examples
This commit is contained in:
parent
a5c08a2041
commit
31cbd9f197
@ -21,7 +21,7 @@ Loop::run(function () {
|
||||
\assert($context instanceof Process);
|
||||
|
||||
// Pipe any data written to the STDOUT in the child process to STDOUT of this process.
|
||||
Amp\Promise\rethrow(ByteStream\pipe($context->getStdout(), new ByteStream\ResourceOutputStream(STDOUT)));
|
||||
Amp\Promise\rethrow(ByteStream\pipe($context->getStdout(), ByteStream\getStdout()));
|
||||
|
||||
print "Waiting 2 seconds to send start data...\n";
|
||||
yield new Delayed(2000);
|
||||
|
@ -20,7 +20,7 @@ Loop::run(function () {
|
||||
\assert($context instanceof Process);
|
||||
|
||||
// Pipe any data written to the STDOUT in the child process to STDOUT of this process.
|
||||
Amp\Promise\rethrow(ByteStream\pipe($context->getStdout(), new ByteStream\ResourceOutputStream(STDOUT)));
|
||||
Amp\Promise\rethrow(ByteStream\pipe($context->getStdout(), ByteStream\getStdout()));
|
||||
|
||||
yield new Delayed(100); // Give the process time to start and access the parcel.
|
||||
|
||||
|
@ -2,7 +2,6 @@
|
||||
<?php
|
||||
require \dirname(__DIR__).'/vendor/autoload.php';
|
||||
|
||||
use Amp\Coroutine;
|
||||
use Amp\Loop;
|
||||
use Amp\Parallel\Example\BlockingTask;
|
||||
use Amp\Parallel\Worker\DefaultPool;
|
||||
@ -18,7 +17,7 @@ $tasks = [
|
||||
];
|
||||
|
||||
// Event loop for parallel tasks
|
||||
Loop::run(function () use (&$results, &$tasks) {
|
||||
Loop::run(function () use (&$results, $tasks) {
|
||||
$timer = Loop::repeat(200, function () {
|
||||
\printf(".");
|
||||
});
|
||||
@ -29,19 +28,15 @@ Loop::run(function () use (&$results, &$tasks) {
|
||||
$coroutines = [];
|
||||
|
||||
foreach ($tasks as $task) {
|
||||
$coroutines[] = function () use ($pool, $task, &$results) {
|
||||
$coroutines[] = Amp\call(function () use ($pool, $task) {
|
||||
$result = yield $pool->enqueue($task);
|
||||
$url = $task->getArgs()[0];
|
||||
\printf("\nRead from %s: %d bytes\n", $url, \strlen($result));
|
||||
$results[$url] = $result;
|
||||
};
|
||||
return $result;
|
||||
});
|
||||
}
|
||||
|
||||
$coroutines = \array_map(function (callable $coroutine): Coroutine {
|
||||
return new Coroutine($coroutine());
|
||||
}, $coroutines);
|
||||
|
||||
yield Amp\Promise\all($coroutines);
|
||||
$results = yield Amp\Promise\all($coroutines);
|
||||
|
||||
return yield $pool->shutdown();
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user