1
0
mirror of https://github.com/danog/parallel.git synced 2024-12-02 09:37:57 +01:00

Update examples

This commit is contained in:
Aaron Piotrowski 2018-10-24 22:31:47 -05:00
parent a5c08a2041
commit 31cbd9f197
No known key found for this signature in database
GPG Key ID: ADD1EF783EDE9EEB
3 changed files with 7 additions and 12 deletions

View File

@ -21,7 +21,7 @@ Loop::run(function () {
\assert($context instanceof Process); \assert($context instanceof Process);
// Pipe any data written to the STDOUT in the child process to STDOUT of this 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"; print "Waiting 2 seconds to send start data...\n";
yield new Delayed(2000); yield new Delayed(2000);

View File

@ -20,7 +20,7 @@ Loop::run(function () {
\assert($context instanceof Process); \assert($context instanceof Process);
// Pipe any data written to the STDOUT in the child process to STDOUT of this 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. yield new Delayed(100); // Give the process time to start and access the parcel.

View File

@ -2,7 +2,6 @@
<?php <?php
require \dirname(__DIR__).'/vendor/autoload.php'; require \dirname(__DIR__).'/vendor/autoload.php';
use Amp\Coroutine;
use Amp\Loop; use Amp\Loop;
use Amp\Parallel\Example\BlockingTask; use Amp\Parallel\Example\BlockingTask;
use Amp\Parallel\Worker\DefaultPool; use Amp\Parallel\Worker\DefaultPool;
@ -18,7 +17,7 @@ $tasks = [
]; ];
// Event loop for parallel tasks // Event loop for parallel tasks
Loop::run(function () use (&$results, &$tasks) { Loop::run(function () use (&$results, $tasks) {
$timer = Loop::repeat(200, function () { $timer = Loop::repeat(200, function () {
\printf("."); \printf(".");
}); });
@ -29,19 +28,15 @@ Loop::run(function () use (&$results, &$tasks) {
$coroutines = []; $coroutines = [];
foreach ($tasks as $task) { foreach ($tasks as $task) {
$coroutines[] = function () use ($pool, $task, &$results) { $coroutines[] = Amp\call(function () use ($pool, $task) {
$result = yield $pool->enqueue($task); $result = yield $pool->enqueue($task);
$url = $task->getArgs()[0]; $url = $task->getArgs()[0];
\printf("\nRead from %s: %d bytes\n", $url, \strlen($result)); \printf("\nRead from %s: %d bytes\n", $url, \strlen($result));
$results[$url] = $result; return $result;
}; });
} }
$coroutines = \array_map(function (callable $coroutine): Coroutine { $results = yield Amp\Promise\all($coroutines);
return new Coroutine($coroutine());
}, $coroutines);
yield Amp\Promise\all($coroutines);
return yield $pool->shutdown(); return yield $pool->shutdown();
}); });