2012-05-06 38 views
2

在Windows XP上運行Example #1 from PHP PHP 5.3.5 curl_multi_select()行將始終在指定的超時期限內阻塞(如果爲空白,則阻塞1秒,如果指定5秒超時,會阻塞5秒),無論獲取內容花費多少時間。我懷疑它與this bug有關。curl_multi_select始終阻止超時值

問題是:什麼是最好的解決方法?我能想到的最好辦法是擺脫curl_multi_select()usleep(x)作爲一種節省一些週期的方法。

回答

2

只要你能忍受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測試的代碼不工作上運行PHP 5.3.20由於this bug in PHP 5.3.18 Amazon實例,這將導致curl_multi_select()調用永遠不會返回以外的任何其他-1

我現在能夠檢索從n個數據在不到30秒的時間內使用200個手柄批量處理1,300個網址。