From 8695d600fc276170c6ecf2c45b17c04145784854 Mon Sep 17 00:00:00 2001 From: Aaron Piotrowski Date: Sun, 27 Sep 2015 22:46:57 -0500 Subject: [PATCH] Update examples with BlockingTask --- composer.json | 1 + examples/BlockingTask.php | 36 ++++++++++++++++++++++++++++++++++++ examples/worker-pool.php | 24 +++++++++++++++++------- examples/worker-process.php | 21 --------------------- examples/worker-thread.php | 21 --------------------- examples/worker.php | 23 +++++++++++++++++++++++ src/Worker/HelloTask.php | 10 ---------- 7 files changed, 77 insertions(+), 59 deletions(-) create mode 100644 examples/BlockingTask.php delete mode 100755 examples/worker-process.php delete mode 100755 examples/worker-thread.php create mode 100755 examples/worker.php delete mode 100644 src/Worker/HelloTask.php diff --git a/composer.json b/composer.json index fea6111..9f86ab2 100755 --- a/composer.json +++ b/composer.json @@ -43,6 +43,7 @@ "autoload-dev": { "psr-4": { "Icicle\\Benchmarks\\Concurrent\\": "benchmarks", + "Icicle\\Examples\\Concurrent\\": "examples", "Icicle\\Tests\\Concurrent\\": "tests" } } diff --git a/examples/BlockingTask.php b/examples/BlockingTask.php new file mode 100644 index 0000000..b72dbc0 --- /dev/null +++ b/examples/BlockingTask.php @@ -0,0 +1,36 @@ +function = $function; + $this->args = array_slice(func_get_args(), 1); + } + + /** + * {@inheritdoc} + */ + public function run(Environment $environment) + { + return call_user_func_array($this->function, $this->args); + } +} diff --git a/examples/worker-pool.php b/examples/worker-pool.php index 0a225f5..9bc6081 100755 --- a/examples/worker-pool.php +++ b/examples/worker-pool.php @@ -2,23 +2,33 @@ start(); + + $results = (yield Promise\all([ + 'google.com' => new Coroutine($pool->enqueue(new BlockingTask('file_get_contents', 'https://google.com'))), + 'icicle.io' => new Coroutine($pool->enqueue(new BlockingTask('file_get_contents', 'https://icicle.io'))), ])); - var_dump($returnValues); + foreach ($results as $source => $result) { + printf("Read from %s: %d bytes\n", $source, strlen($result)); + } + + yield $pool->shutdown(); }; $coroutine = new Coroutine($generator()); $coroutine->done(); +Loop\periodic(0.1, function () { + printf(".\n"); +})->unreference(); + Loop\run(); diff --git a/examples/worker-process.php b/examples/worker-process.php deleted file mode 100755 index 911d4df..0000000 --- a/examples/worker-process.php +++ /dev/null @@ -1,21 +0,0 @@ -#!/usr/bin/env php -start(); - - $returnValue = (yield $worker->enqueue(new HelloTask())); - printf("Return value: %s\n", $returnValue); - - $code = (yield $worker->shutdown()); - printf("Code: %d\n", $code); -})->done(); - -Loop\run(); diff --git a/examples/worker-thread.php b/examples/worker-thread.php deleted file mode 100755 index 3992494..0000000 --- a/examples/worker-thread.php +++ /dev/null @@ -1,21 +0,0 @@ -#!/usr/bin/env php -start(); - - $returnValue = (yield $worker->enqueue(new HelloTask())); - printf("Return value: %s\n", $returnValue); - - $code = (yield $worker->shutdown()); - printf("Code: %d\n", $code); -})->done(); - -Loop\run(); diff --git a/examples/worker.php b/examples/worker.php new file mode 100755 index 0000000..851d8be --- /dev/null +++ b/examples/worker.php @@ -0,0 +1,23 @@ +#!/usr/bin/env php +create(); + $worker->start(); + + $result = (yield $worker->enqueue(new BlockingTask('file_get_contents', 'https://google.com'))); + printf("Read %d bytes\n", strlen($result)); + + $code = (yield $worker->shutdown()); + printf("Code: %d\n", $code); +})->done(); + +Loop\run(); diff --git a/src/Worker/HelloTask.php b/src/Worker/HelloTask.php deleted file mode 100644 index e94471e..0000000 --- a/src/Worker/HelloTask.php +++ /dev/null @@ -1,10 +0,0 @@ -