protected static function processBatchTimesChunk($chunk, $timeout) { $pid = pcntl_fork(); if ($pid>0) { // parent return; } // child or could not fork foreach ($chunk as $NaPTAN) { self::fetchData($NaPTAN, $timeout); } if ($pid != -1) exit; } public static function preCacheTimes(array $NaPTANs, $threads=5, $timeout=15) { if (!count($NaPTANs)) return array(); $chunks = array_chunk($NaPTANs, ceil(count($NaPTANs) / $threads)); // spawn threads foreach ($chunks as $chunk) { self::processBatchTimesChunk($chunk, $timeout); } // wait for completion $status=0; while (pcntl_wait($status, WNOHANG)>=0) { /* do nothing */ } }