curl_multi_select总是阻塞超时值

时间:2012-05-06 21:08:45

标签: php curl curl-multi

在Windows XP PHP 5.3.5上运行Example #1 from PHP时,curl_multi_select()行将始终在指定的超时时间内阻塞(如果为空,它将阻止1秒,如果我指定5秒超时无论获取内容所需的时间如何,它都会阻止5秒钟。我怀疑它与this bug有关。

问题是:最好的工作是什么?我能想到的最好的方法是摆脱curl_multi_select()usleep(x),以节省一些周期。

1 个答案:

答案 0 :(得分:3)

只要您可以忍受1秒钟阻止,这可能会有所帮助。

manual page for curl_multi_select上有一条评论提到此阻止持续到至少一个连接已完成或$timeout秒为止,以先发生者为准。他们还写道,应该包含对curl_multi_select的调用:

private function full_curl_multi_exec($mh, &$still_running) {
        do {
                $rv = curl_multi_exec($mh, $still_running);
        } while ($rv == CURLM_CALL_MULTI_PERFORM);
        return $rv;
}

然后修改检查正在运行的句柄的循环:

// execute the handles
$still_running = null;
$this->full_curl_multi_exec($mh, $still_running);

// check whether the handles have finished
do { // "wait for completion"-loop 
        curl_multi_select($mh); // non-busy (!) wait for state change 
        $this->full_curl_multi_exec($mh, $still_running); // get new state
        while ($info = curl_multi_info_read($mh)) { 
            // process completed request (e.g. curl_multi_getcontent($info['handle'])) 
        }
} while ($still_running);

在此修改之前,使用PHP 5.4测试的代码由于this bug in PHP 5.3.18而无法运行运行PHP 5.3.20的Amazon实例,这导致调用curl_multi_select()永远不会返回除-1

我现在能够使用200个手柄批次在不到30秒的时间内从近1,300个网址中检索数据。