From b9d6720c832ebc9805de438901828045334f52d0 Mon Sep 17 00:00:00 2001 From: Pieter Hordijk Date: Thu, 8 Nov 2018 23:29:07 +0300 Subject: [PATCH] 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) --- examples/worker-pool.php | 13 ++++++------- examples/worker.php | 2 +- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/examples/worker-pool.php b/examples/worker-pool.php index d21216c..deb843b 100755 --- a/examples/worker-pool.php +++ b/examples/worker-pool.php @@ -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; }); } diff --git a/examples/worker.php b/examples/worker.php index 16e6868..4aa946d 100755 --- a/examples/worker.php +++ b/examples/worker.php @@ -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();