mirror of
https://github.com/danog/parallel.git
synced 2024-11-29 20:29:00 +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
|
||||
$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;
|
||||
});
|
||||
}
|
||||
|
@ -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();
|
||||
|
Loading…
Reference in New Issue
Block a user