Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Dave Ingram

By: a guest on Nov 10th, 2009  |  syntax: PHP  |  size: 0.72 KB  |  views: 37  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.   protected static function processBatchTimesChunk($chunk, $timeout) {
  2.     $pid = pcntl_fork();
  3.     if ($pid>0) {
  4.       // parent
  5.       return;
  6.     }
  7.     // child or could not fork
  8.     foreach ($chunk as $NaPTAN) {
  9.       self::fetchData($NaPTAN, $timeout);
  10.     }
  11.     if ($pid != -1) exit;
  12.   }
  13.  
  14.   public static function preCacheTimes(array $NaPTANs, $threads=5, $timeout=15) {
  15.     if (!count($NaPTANs)) return array();
  16.     $chunks = array_chunk($NaPTANs, ceil(count($NaPTANs) / $threads));
  17.     // spawn threads
  18.     foreach ($chunks as $chunk) {
  19.       self::processBatchTimesChunk($chunk, $timeout);
  20.     }
  21.     // wait for completion
  22.     $status=0;
  23.     while (pcntl_wait($status, WNOHANG)>=0) { /* do nothing */ }
  24.   }
clone this paste RAW Paste Data