/** * * @return ResultCollection */ public function getResultCollection() { $pool = new WorkerPool(); $pool->setWorkerPoolSize($this->processCollection->count())->create(new ClosureWorker(function (Process $process) { $result = new Result(); $result->setProcess($process); try { if (!class_exists($process->getClass())) { throw new \Exception(sprintf('Invalid class provided: "%s"', $process->getClass())); } $class = $process->getClass(); $class = new $class(); $result->setData($class->process($process->getData())); $result->setSuccessful(true); } catch (\Exception $e) { $result->setErrorMessage($e->getMessage()); } return $result; })); foreach ($this->processCollection as $process) { $pool->run($process); } $pool->waitForAllWorkers(); $resultCollection = new ResultCollection(); foreach ($pool as $result) { $resultEntity = new Result(); if (!empty($result['data'])) { $resultEntity = $result['data']; } elseif (!empty($result['workerException']['message'])) { $resultEntity->setErrorMessage($result['workerException']['message']); } elseif (!empty($result['poolException']['message'])) { $resultEntity->setErrorMessage($result['poolException']['message']); } else { $resultEntity->setErrorMessage(self::INTERNAL_ERROR); } $resultCollection->add($resultEntity); } return $resultCollection; }
public function testPingWorkers() { try { $wp = new WorkerPool(); $wp->setWorkerPoolSize(50); $wp->create(new Fixtures\PingWorker()); $failCount = 0; for ($i = 0; $i < 500; $i++) { $wp->run($i); $a = $wp->getFreeAndBusyWorkers(); if ($a['free'] + $a['busy'] != $a['total']) { $failCount++; } } $wp->waitForAllWorkers(); $this->assertLessThanOrEqual(0, $failCount, 'Sometimes the sum of free and busy workers does not equal to the pool size.'); $this->assertEquals(500, count($wp), 'The result count should be 500.'); $i = 0; foreach ($wp as $val) { $i++; } $this->assertEquals(500, $i, 'We should have 500 results in the pool.'); $this->assertEquals(0, count($wp), 'The result count should be 0 now.'); } catch (\Exception $e) { $this->assertTrue(FALSE, 'An unexpected exception was thrown.'); } try { $wp->destroy(); } catch (\Exception $e) { $this->assertTrue(FALSE, 'WorkerPool::Destroy shouldn\\t throw an exception of type ' . get_class($e) . ' with message:' . $e->getMessage() . "\n" . $e->getTraceAsString()); } }
distinct several conditions Begin third task... Task 1 -- The longest word is characteristically Task 3 -- The word "species" occurs 1927 times. Returned from Parallel.Invoke Press any key to exit */ $wp = new WorkerPool(); $wp->setWorkerPoolSize(4)->create(new SuperClosureWorker()); // Retrieve Darwin's "Origin of the Species" from Gutenberg.org. $words = CreateWordArray('darwin.txt'); //$words = CreateWordArray('http://www.gutenberg.org/files/2009/2009.txt'); $wp->run(new SerializableWorkerClosure(function ($words, $semaphore, $storage) { echo "Begin Task Count Words\n"; echo sprintf("Task Count Words -- We have %d words\n", count($words)); }, $words)); $wp->run(new SerializableWorkerClosure(function ($words, $semaphore, $storage) { echo "Begin Task Longest Word\n"; \ParallelTasks\GetLongestWord($words); }, $words)); $wp->run(new SerializableWorkerClosure(function ($words, $semaphore, $storage) { echo "Begin Task Most Common Words\n"; \ParallelTasks\GetMostCommonWords($words); }, $words)); $wp->run(new SerializableWorkerClosure(function ($words, $semaphore, $storage) { echo "Begin Task Count For Word\n"; \ParallelTasks\GetCountForWord($words, "species"); }, $words));
<?php /** * Thisfile requires the jeremeamia/SuperClosure */ require_once __DIR__ . '/../autoload.php'; if (file_exists(__DIR__ . '/../../../autoload.php')) { require_once __DIR__ . '/../../../autoload.php'; } else { die("Cannot find a classloader for jeremeamia/SuperClosure!\n"); } use QXS\WorkerPool\WorkerPool, QXS\WorkerPool\SuperClosureWorker, QXS\WorkerPool\SerializableWorkerClosure; $wp = new WorkerPool(); $wp->setWorkerPoolSize(4)->create(new SuperClosureWorker()); for ($i = 0; $i < 10; $i++) { $wp->run(new SerializableWorkerClosure(function ($input, $semaphore, $storage) use($i) { if ($i % 2) { echo "CHILD " . getmypid() . " CODE using {$i} - received input {$input} ...\n"; } else { echo "child " . getmypid() . " Code using {$i} - received input {$input} ...\n"; } return $input / 2; }, $i * 2)); } $wp->waitForAllWorkers(); // wait for all workers foreach ($wp as $val) { echo "MASTER RECEIVED " . $val['data'] . "\n"; }
/** * Thisfile requires the jeremeamia/SuperClosure */ require_once __DIR__ . '/../autoload.php'; if (file_exists(__DIR__ . '/../../../autoload.php')) { require_once __DIR__ . '/../../../autoload.php'; } else { die("Cannot find a classloader for jeremeamia/SuperClosure!\n"); } use QXS\WorkerPool\WorkerPool, QXS\WorkerPool\SuperClosureWorker, QXS\WorkerPool\SerializableWorkerClosure; $wp = new WorkerPool(); $wp->setWorkerPoolSize(4)->create(new SuperClosureWorker()); echo "starting closure 1..\n"; $wp->run(new SerializableWorkerClosure(function ($input, $semaphore, $storage) { sleep(2); echo "{$input}: Hi {$input}\n"; }, 1)); echo "starting closure 2..\n"; $z = "hello"; $wp->run(new SerializableWorkerClosure(function ($input, $semaphore, $storage) use($z) { sleep(2); echo "{$input}: {$z} {$input}\n"; }, 2)); echo "starting closure 3..\n"; $wp->run(new SerializableWorkerClosure(function ($input, $semaphore, $storage) use($z) { sleep(2); echo "{$input}: {$z} "; $input *= 2; echo $input . "\n"; }, 3)); echo "starting closure 4..\n";
echo "\t[" . getmypid() . "] Hi {$input}\n"; sleep(mt_rand(0, 10)); // this is the workload! // and sometimes exceptions might occur if (mt_rand(0, 10) == 9) { throw new \RuntimeException('We have a problem for ' . $input . '.'); } return "Hi {$input}"; // return null here, in case you do not want to pass any data to the parent } } $wp = new WorkerPool(); $wp->setWorkerPoolSize(10)->create(new MyWorker()); // produce some tasks for ($i = 1; $i <= 50; $i++) { $wp->run($i); } // some statistics echo "Busy Workers:" . $wp->getBusyWorkers() . " Free Workers:" . $wp->getFreeWorkers() . "\n"; // wait for completion of all tasks $wp->waitForAllWorkers(); // collect all the results foreach ($wp as $val) { if (isset($val['data'])) { echo "RESULT: " . $val['data'] . "\n"; } elseif (isset($val['workerException'])) { echo "WORKER EXCEPTION: " . $val['workerException']['class'] . ": " . $val['workerException']['message'] . "\n" . $val['workerException']['trace'] . "\n"; } elseif (isset($val['poolException'])) { echo "POOL EXCEPTION: " . $val['poolException']['class'] . ": " . $val['poolException']['message'] . "\n" . $val['poolException']['trace'] . "\n"; } }
list($part_number, $part_step) = $input; $sum = '0'; for ($i = $part_number; $i < $this->num_steps; $i += $part_step) { //$x = ($i + 0.5) * $this->step; //$sum += 4.0 / (1.0 + $x * $x); $x = bcmul(bcadd($i, '0.5'), $this->step); $sum = bcadd($sum, bcdiv('4.0', bcadd('1.0', bcmul($x, $x)))); } return $sum; } } bcscale(100); // sets the scale $num_steps = 1000000; // setps - sets the calculation detail $part_step = 8; // parallelization degree set to number of cores $step = bcdiv('1.0', (string) $num_steps); $wp = new WorkerPool(); $wp->setWorkerPoolSize($part_step)->create(new PiWorker($num_steps, $step)); for ($i = 0; $i < $part_step; $i++) { $wp->run(array($i, $part_step)); } $wp->waitForAllWorkers(); // wait for all workers $sum = '0'; foreach ($wp as $result) { $sum = bcadd($sum, $result['data']); } $pi = bcmul($step, $sum); echo "PI is {$pi}\n";