PHP_Fork

pecl の threadsの続きっぽいこと。
Threads in PHPという記事を見たら、PHP_Forkについて書かれてあったので、それを使ってやってみようかな、と。

[php] <?php require_once('PHP/Fork.php');

class thread extends PHP_Fork { var $url; function thread($name, $url, $t) { $this->PHP_Fork($name); $this->url = $url; } function run() { $res = file_get_contents($this->url); $this->setVariable('out', $res); } function getOut() { return $this->getVariable('out'); } }

$urls = array( 'http://search.yahooapis.com/VideoSearchService/V1/videoSearch?appid=YahooDemo&query=Pearl+Jam&output=json', 'http://search.yahooapis.com/ImageSearchService/V1/imageSearch?appid=YahooDemo&query=Pearl+Jam&output=json', 'http://search.yahooapis.com/AudioSearchService/V1/artistSearch?appid=YahooDemo&artist=Pearl+Jam&output=json' );

$ths = array(); foreach ($urls as $row) { $th = new thread(rand(), $row, $t); $th->start(); $ths = $th; }

$contents = array(); while(TRUE) { $tmp = array(); foreach ($ths as $th) { if ($th->isActive()) { $tmp = $th; } else { $contents[] = $th->getOut(); } } if (empty($tmp)) { break; } $ths = $tmp; }

print_r($contents);[/php]
結論から言えば、コードがまずいので、このままでは動作しない。。。
とりあえず、

$this->setVariable('out', $res);

の箇所で「共有メモリが足りない」と怒られる。

というか、やっぱりforkなんで、スレッド処理・・・run() の部分の起動までが遅い。
サーバみたいなものならともかく、短い処理を実行する使い捨てのワーカスレッドとして使用するにはツラそう。