$tasks = array( new job('0'), new job('1'), new job('2'), new job('3'), new job('4'), new job('5'), new job('6'), new job('7'), new job('8'), new job('9'), new job('10'),);// Add tasks to pool queueforeach ($tasks as $task) {$p->submit($task);}

Please note, when using the collect function, it's important that you extend the pool class so you can keep checking for finished threads until they're all done.

<?phpclass TestWork extends Threaded { protected $complete;//$pData is the data sent to your worker thread to do it's job.public function __construct($pData){//transfer all the variables to local variables$this->complete = false;$this->testData = $pData; }//This is where all of your work will be done.public function run(){usleep(2000000); //sleep 2 seconds to simulate a large job$this->complete = true; } public function isGarbage() { return $this->complete; }}class ExamplePool extends Pool{ public $data = array(); public function process() {// Run this loop as long as we have // jobs in the poolwhile (count($this->work)) {$this->collect(function (TestWork $task) {// If a task was marked as done // collect its resultsif ($task->isGarbage()) {$tmpObj = new stdclass();$tmpObj->complete = $task->complete;//this is how you get your completed data back out [accessed by $pool->process()]$this->data[] = $tmpObj; } return $task->isGarbage(); }); }// All jobs are done // we can shutdown the pool$this->shutdown(); return $this->data; }}$pool = new ExamplePool(3);$testData = 'asdf';for($i=0;$i<5;$i++) {$pool->submit(new TestWork($testData));}$retArr = $pool->process(); //get all of the resultsecho '<pre>';print_r($retArr); //return the array of results (and maybe errors)echo '</pre>';?>

$spammed_id = -1;for ($i = 1; $i <= 100; ++$i){ // add some jobsif ($spammed_id == -1 && ($x = $pool->worker_list())!= null && @$x[2]){$spammed_id = $x[2]; echo "spamming worker {$spammed_id} with lots of tasks from now on\n"; } if ($spammed_id != -1 && ($i % 5) == 0) // every 5th job is routed to one worker, so it has 20% of the total jobs (with 3 workers it should do ~33%, not it has (33+20)%, so only delegate to worker if you plan to do balancing as well... )$pool->submitTo($spammed_id,new Task(10*$i)); else$pool->submit(new Task(10*$i));}$pool->shutdown();var_dump($config); // "val" is exactly 100000, "val2" is probably a bit less// also: if you disable the spammer, you'll that the order of the "arr" is random.?>