// 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"; } } // write something, before the parent exits echo "ByeBye\n";