From 7485b3c9a09106442e78bcd4346162b3744815cc Mon Sep 17 00:00:00 2001 From: Alexander Cheprasov Date: Wed, 20 Apr 2016 20:29:05 +0100 Subject: [PATCH] dev-1.1.0 --- README.md | 13 +++++++------ composer.json | 5 ++--- examples/example.php | 11 ++++++----- src/Parallel/Parallel.php | 13 ++++++++----- tests/Integration/ParallelTest.php | 18 ++++++++++++++++++ 5 files changed, 41 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 4c86811..438ae0d 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,4 @@ -# Parallel v1.0.0 for PHP >= 5.5 +# Parallel v1.1.0 for PHP >= 5.5 The class allows you to run multiple operations parallel in different processes and send results to the main process. Useful if you need to run multiple independent operations simultaneously, instead of sequential execution, or if you run several independent queries, for example, queries to different data bases. @@ -8,10 +8,9 @@ The class allows you to run multiple operations parallel in different processes run('obj', function() { // do some thing ... sleep(2); -// waiting for and -// and get results +// waiting for and and get results. +// use wait() without parameters for wait all forks. Example: $Parallel->wait(); $result = $Parallel->wait(['foo', 'obj']); + print_r($result); -// 3 parallel operations by 2 seconds take about 2 seconds, instead 6 seconds. print_r(microtime(true) - $time); +// 3 parallel operations by 2 seconds take about 2 seconds, instead 6 seconds. + // Array // ( // [foo] => Array diff --git a/composer.json b/composer.json index cf8a439..4c119bb 100644 --- a/composer.json +++ b/composer.json @@ -1,6 +1,6 @@ { "name": "cheprasov/php-parallel", - "version": "1.0.0", + "version": "1.1.0", "description": "The class allows you to run multiple operations parallel in different processes and send results to the main process. Useful if you need to run multiple independent operations simultaneously, instead of sequential execution, or if you run several independent queries, for example, queries to different data bases", "homepage": "http://github.com/cheprasov/php-parallel", "minimum-stability": "stable", @@ -20,7 +20,6 @@ "php": ">=5.5" }, "require-dev": { - "phpunit/phpunit": "5.1.*", - "cheprasov/php-simple-profiler": "master-dev" + "phpunit/phpunit": "4.8.*" } } diff --git a/examples/example.php b/examples/example.php index ab522e2..30c6876 100644 --- a/examples/example.php +++ b/examples/example.php @@ -9,10 +9,9 @@ * file that was distributed with this source code. */ require (dirname(__DIR__).'/vendor/autoload.php'); - use Parallel\Parallel; -// EXAMPLE, how run parallel 3 operations. +// EXAMPLE, how to run parallel 3 operations. // Using Parallel via ApcuStorage (APCu, see http://php.net/manual/ru/book.apcu.php) $Parallel = new Parallel(new \Parallel\Storage\ApcuStorage()); @@ -43,12 +42,14 @@ // do some thing ... sleep(2); -// waiting for and -// and get results +// waiting for and and get results. +// use wait() without parameters for wait all forks. Example: $Parallel->wait(); $result = $Parallel->wait(['foo', 'obj']); + print_r($result); -// 3 parallel operations by 2 seconds take about 2 seconds, instead 6 seconds. print_r(microtime(true) - $time); +// 3 parallel operations by 2 seconds take about 2 seconds, instead 6 seconds. + // Array // ( // [foo] => Array diff --git a/src/Parallel/Parallel.php b/src/Parallel/Parallel.php index 370285e..99dc46d 100644 --- a/src/Parallel/Parallel.php +++ b/src/Parallel/Parallel.php @@ -14,7 +14,7 @@ class Parallel { - const VERSION = '1.0.0'; + const VERSION = '1.1.0'; /** * @var StorageInterface @@ -69,13 +69,16 @@ public function run($name, $callback) { } /** - * Wait fork by names - * @param string|string[] $names + * Wait fork by names or all (without parameters) + * @param string|string[]|null $names * @return array */ - public function wait($names) { - $namesArr = (array) $names; + public function wait($names = null) { + $namesArr = !isset($names) ? array_keys($this->pids) : (array) $names; foreach ($namesArr as $name) { + if (!isset($this->pids[$name])) { + continue; + } pcntl_waitpid($this->pids[$name], $status); unset($this->pids[$name]); } diff --git a/tests/Integration/ParallelTest.php b/tests/Integration/ParallelTest.php index 3b04894..8e9df24 100644 --- a/tests/Integration/ParallelTest.php +++ b/tests/Integration/ParallelTest.php @@ -56,6 +56,24 @@ public function test_viaMemcachedStorage() { $this->assertSame(['n:1' => 1, 'n:2' => 4, 'n:3' => 9, 'n:4' => 16, 'n:5' => 25], $result); } + public function test_waitAllViaMemcachedStorage() { + $Parallel = new Parallel(new MemcachedStorage(['servers'=>[explode(':', TEST_MEMCACHED_SERVER)]])); + + $time = microtime(true); + for ($i = 1; $i <= 5; ++$i) { + $Parallel->run('n:'. $i, function() use ($i) { + sleep($i); + return $i * $i; + }); + } + $result = $Parallel->wait(); + $time = microtime(true) - $time; + $this->assertGreaterThanOrEqual(5, $time); + $this->assertLessThan(6, $time); + + $this->assertSame(['n:1' => 1, 'n:2' => 4, 'n:3' => 9, 'n:4' => 16, 'n:5' => 25], $result); + } + public function test_nestedViaApcuStorage() { $Parallel = new Parallel(new ApcuStorage());