mirror of
https://github.com/danog/parallel.git
synced 2024-12-02 09:37:57 +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:
parent
f56e461200
commit
b9d6720c83
@ -12,9 +12,9 @@ $results = [];
|
|||||||
|
|
||||||
// We can first define tasks and then run them
|
// We can first define tasks and then run them
|
||||||
$tasks = [
|
$tasks = [
|
||||||
new CallableTask('file_get_contents', 'http://php.net'),
|
new CallableTask('file_get_contents', ['http://php.net']),
|
||||||
new CallableTask('file_get_contents', 'https://amphp.org'),
|
new CallableTask('file_get_contents', ['https://amphp.org']),
|
||||||
new CallableTask('file_get_contents', 'https://github.com'),
|
new CallableTask('file_get_contents', ['https://github.com']),
|
||||||
];
|
];
|
||||||
|
|
||||||
// Event loop for parallel tasks
|
// Event loop for parallel tasks
|
||||||
@ -28,11 +28,10 @@ Loop::run(function () use (&$results, $tasks) {
|
|||||||
|
|
||||||
$coroutines = [];
|
$coroutines = [];
|
||||||
|
|
||||||
foreach ($tasks as $task) {
|
foreach ($tasks as $index => $task) {
|
||||||
$coroutines[] = Amp\call(function () use ($pool, $task) {
|
$coroutines[] = Amp\call(function () use ($pool, $index, $task) {
|
||||||
$result = yield $pool->enqueue($task);
|
$result = yield $pool->enqueue($task);
|
||||||
$url = $task->getArgs()[0];
|
\printf("\nRead from task %d: %d bytes\n", $index, \strlen($result));
|
||||||
\printf("\nRead from %s: %d bytes\n", $url, \strlen($result));
|
|
||||||
return $result;
|
return $result;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -10,7 +10,7 @@ Amp\Loop::run(function () {
|
|||||||
|
|
||||||
$worker = $factory->create();
|
$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));
|
\printf("Read %d bytes\n", \strlen($result));
|
||||||
|
|
||||||
$code = yield $worker->shutdown();
|
$code = yield $worker->shutdown();
|
||||||
|
Loading…
Reference in New Issue
Block a user