Skip to content

Commit

Permalink
Merge pull request #10 from cheprasov/dev-1.1.0
Browse files Browse the repository at this point in the history
dev-1.1.0
  • Loading branch information
cheprasov committed Apr 20, 2016
2 parents d71f20a + 7485b3c commit 4f312d3
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 19 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -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.

Expand All @@ -8,10 +8,9 @@ The class allows you to run multiple operations parallel in different processes
<?php

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());
Expand Down Expand Up @@ -42,12 +41,14 @@ $Parallel->run('obj', function() {
// do some thing ...
sleep(2);

// waiting for <foo> and <obj>
// and get results
// waiting for <foo> and <obj> 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
Expand Down
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -20,7 +20,6 @@
"php": ">=5.5"
},
"require-dev": {
"phpunit/phpunit": "5.1.*",
"cheprasov/php-simple-profiler": "master-dev"
"phpunit/phpunit": "4.8.*"
}
}
11 changes: 6 additions & 5 deletions examples/example.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -43,12 +42,14 @@
// do some thing ...
sleep(2);

// waiting for <foo> and <obj>
// and get results
// waiting for <foo> and <obj> 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
Expand Down
13 changes: 8 additions & 5 deletions src/Parallel/Parallel.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

class Parallel {

const VERSION = '1.0.0';
const VERSION = '1.1.0';

/**
* @var StorageInterface
Expand Down Expand Up @@ -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]);
}
Expand Down
18 changes: 18 additions & 0 deletions tests/Integration/ParallelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down

0 comments on commit 4f312d3

Please sign in to comment.