1
0
mirror of https://github.com/danog/parallel.git synced 2024-11-30 04:39:01 +01:00

Fixed examples (#61)

* Fixed examples

CallableTask expects the parameters to be passed in as array

* Fixed worker-pool example

The task does not make available the arguments (anymore)
This commit is contained in:
Pieter Hordijk 2018-11-08 23:29:07 +03:00 committed by Aaron Piotrowski
parent f56e461200
commit b9d6720c83
2 changed files with 7 additions and 8 deletions

View File

@ -12,9 +12,9 @@ $results = [];
// We can first define tasks and then run them
$tasks = [
new CallableTask('file_get_contents', 'http://php.net'),
new CallableTask('file_get_contents', 'https://amphp.org'),
new CallableTask('file_get_contents', 'https://github.com'),
new CallableTask('file_get_contents', ['http://php.net']),
new CallableTask('file_get_contents', ['https://amphp.org']),
new CallableTask('file_get_contents', ['https://github.com']),
];
// Event loop for parallel tasks
@ -28,11 +28,10 @@ Loop::run(function () use (&$results, $tasks) {
$coroutines = [];
foreach ($tasks as $task) {
$coroutines[] = Amp\call(function () use ($pool, $task) {
foreach ($tasks as $index => $task) {
$coroutines[] = Amp\call(function () use ($pool, $index, $task) {
$result = yield $pool->enqueue($task);
$url = $task->getArgs()[0];
\printf("\nRead from %s: %d bytes\n", $url, \strlen($result));
\printf("\nRead from task %d: %d bytes\n", $index, \strlen($result));
return $result;
});
}

View File

@ -10,7 +10,7 @@ Amp\Loop::run(function () {
$worker = $factory->create();
$result = yield $worker->enqueue(new CallableTask('file_get_contents', 'https://google.com'));
$result = yield $worker->enqueue(new CallableTask('file_get_contents', ['https://google.com']));
\printf("Read %d bytes\n", \strlen($result));
$code = yield $worker->shutdown();