当我听到有人使用curl_multi_exec时,通常会发现他们只是用100个网址加载它,然后在完成后等待,然后全部处理它们,然后用接下来的100个网址重新开始…怪我,我是这样做,但后来我发现有可能在某些事情仍在进行时删除/添加curl_multi的句柄,并且它确实节省了大量时间,特别是如果你重用已经打开的连接.我写了一个小型库来处理带回调的请求队列;我当然不是在这里发布完整版本(“小”仍然是相当多的代码),但这里有一个简化版本,主要给你一般的想法:
public function launch() {
$channels = $freeChannels = array_fill(0, $this->maxConnections, NULL);
$activeJobs = array();
$running = 0;
do {
// pick jobs for free channels:
while ( !(empty($freeChannels) || empty($this->jobQueue)) ) {
// take free channel, (re)init curl handle and let
// queued object set options
$chId = key($freeChannels);
if (empty($channels[$chId])) {
$channels[$chId] = curl_init();
}
$job = array_pop($this->jobQueue);
$job->init($channels[$chId]);
curl_multi_add_handle($this->master, $channels[$chId]);
$activeJobs[$chId] = $job;
unset($freeChannels[$chId]);
}
$pending = count($activeJobs);
// launch them:
if ($pending > 0) {
while(($mrc = curl_multi_exec($this->master, $running)) == CURLM_CALL_MULTI_PERFORM);
// poke it while it wants
curl_multi_select($this->master);
// wait for some activity, don't eat CPU
while ($running < $pending && ($info = curl_multi_info_read($this->master))) {
// some connection(s) finished, locate that job and run response handler:
$pending--;
$chId = array_search($info['handle'], $channels);
$content = curl_multi_getcontent($channels[$chId]);
curl_multi_remove_handle($this->master, $channels[$chId]);
$freeChannels[$chId] = NULL;
// free up this channel
if ( !array_key_exists($chId, $activeJobs) ) {
// impossible, but...
continue;
}
$activeJobs[$chId]->onComplete($content);
unset($activeJobs[$chId]);
}
}
} while ( ($running > 0 && $mrc == CURLM_OK) || !empty($this->jobQueue) );
}
在我的版本中,$jobs实际上是单独的类,而不是控制器或模型的实例.他们只处理设置cURL选项,解析响应并调用给定的回调onComplete.
使用此结构,只要池中的某些内容完成,新请求就会立即启动.
当然,如果不只是检索需要时间而且处理也不会真正拯救你……而且它并不是真正的并行处理.但我仍然希望它有所帮助. 🙂
附:为我做了一招. :)一旦8小时工作现在使用50个连接池完成3-4分钟.无法形容那种感觉. :)我真的不希望它按计划工作,因为使用PHP它很少像预期的那样工作……这就像“好吧,希望它至少在一个小时内完成……哇……等等..已经?!8-O“