|
1. 經(jīng)典cURL并發(fā)機(jī)制及其存在的問題
經(jīng)典的cURL實(shí)現(xiàn)機(jī)制在網(wǎng)上很容易找到, 比如參考php在線手冊(cè)的如下實(shí)現(xiàn)方式:
復(fù)制代碼 代碼如下:
function classic_curl($urls, $delay) {
$queue = curl_multi_init();
$map = array();
foreach ($urls as $url) {
// create cURL resources
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_NOSIGNAL, true);
// add handle
curl_multi_add_handle($queue, $ch);
$map[$url] = $ch;
}
$active = null;
// execute the handles
do {
$mrc = curl_multi_exec($queue, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
while ($active > 0 && $mrc == CURLM_OK) {
if (curl_multi_select($queue, 0.5) != -1) {
do {
$mrc = curl_multi_exec($queue, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
}
}
$responses = array();
foreach ($map as $url=>$ch) {
$responses[$url] = callback(curl_multi_getcontent($ch), $delay);
curl_multi_remove_handle($queue, $ch);
curl_close($ch);
}
curl_multi_close($queue);
return $responses;
}
首先將所有的URL壓入并發(fā)隊(duì)列, 然后執(zhí)行并發(fā)過程, 等待所有請(qǐng)求接收完之后進(jìn)行數(shù)據(jù)的解析等后續(xù)處理. 在實(shí)際的處理過程中, 受網(wǎng)絡(luò)傳輸?shù)挠绊? 部分URL的內(nèi)容會(huì)優(yōu)先于其他URL返回, 但是經(jīng)典cURL并發(fā)必須等待最慢的那個(gè)URL返回之后才開始處理, 等待也就意味著CPU的空閑和浪費(fèi). 如果URL隊(duì)列很短, 這種空閑和浪費(fèi)還處在可接受的范圍, 但如果隊(duì)列很長(zhǎng), 這種等待和浪費(fèi)將變得不可接受.
2. 改進(jìn)的Rolling cURL并發(fā)方式
仔細(xì)分析不難發(fā)現(xiàn)經(jīng)典cURL并發(fā)還存在優(yōu)化的空間, 優(yōu)化的方式時(shí)當(dāng)某個(gè)URL請(qǐng)求完畢之后盡可能快的去處理它, 邊處理邊等待其他的URL返回, 而不是等待那個(gè)最慢的接口返回之后才開始處理等工作, 從而避免CPU的空閑和浪費(fèi). 閑話不多說, 下面貼上具體的實(shí)現(xiàn):
復(fù)制代碼 代碼如下:
function rolling_curl($urls, $delay) {
$queue = curl_multi_init();
$map = array();
foreach ($urls as $url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_NOSIGNAL, true);
curl_multi_add_handle($queue, $ch);
$map[(string) $ch] = $url;
}
$responses = array();
do {
while (($code = curl_multi_exec($queue, $active)) == CURLM_CALL_MULTI_PERFORM) ;
if ($code != CURLM_OK) { break; }
// a request was just completed -- find out which one
while ($done = curl_multi_info_read($queue)) {
// get the info and content returned on the request
$info = curl_getinfo($done['handle']);
$error = curl_error($done['handle']);
$results = callback(curl_multi_getcontent($done['handle']), $delay);
$responses[$map[(string) $done['handle']]] = compact('info', 'error', 'results');
// remove the curl handle that just completed
curl_multi_remove_handle($queue, $done['handle']);
curl_close($done['handle']);
}
// Block for data in / output; error handling is done by curl_multi_exec
if ($active > 0) {
curl_multi_select($queue, 0.5);
}
} while ($active);
curl_multi_close($queue);
return $responses;
}
3. 兩種并發(fā)實(shí)現(xiàn)的性能對(duì)比
改進(jìn)前后的性能對(duì)比試驗(yàn)在LINUX主機(jī)上進(jìn)行, 測(cè)試時(shí)使用的并發(fā)隊(duì)列如下:
http://item.taobao.com/item.htm?id=14392877692
http://item.taobao.com/item.htm?id=16231676302
http://item.taobao.com/item.htm?id=17037160462
http://item.taobao.com/item.htm?id=5522416710
http://item.taobao.com/item.htm?id=16551116403
http://item.taobao.com/item.htm?id=14088310973
簡(jiǎn)要說明下實(shí)驗(yàn)設(shè)計(jì)的原則和性能測(cè)試結(jié)果的格式: 為保證結(jié)果的可靠, 每組實(shí)驗(yàn)重復(fù)20次, 在單次實(shí)驗(yàn)中, 給定相同的接口URL集合, 分別測(cè)量Classic(指經(jīng)典的并發(fā)機(jī)制)和Rolling(指改進(jìn)后的并發(fā)機(jī)制)兩種并發(fā)機(jī)制的耗時(shí)(秒為單位), 耗時(shí)短者勝出(Winner), 并計(jì)算節(jié)省的時(shí)間(Excellence, 秒為單位)以及性能提升比例(Excel. %). 為了盡量貼近真實(shí)的請(qǐng)求而又保持實(shí)驗(yàn)的簡(jiǎn)單, 在對(duì)返回結(jié)果的處理上只是做了簡(jiǎn)單的正則表達(dá)式匹配, 而沒有進(jìn)行其他復(fù)雜的操作. 另外, 為了確定結(jié)果處理回調(diào)對(duì)性能對(duì)比測(cè)試結(jié)果的影響, 可以使用usleep模擬現(xiàn)實(shí)中比較負(fù)責(zé)的數(shù)據(jù)處理邏輯(如提取, 分詞, 寫入文件或數(shù)據(jù)庫(kù)等).
性能測(cè)試中用到的回調(diào)函數(shù)為:
復(fù)制代碼 代碼如下:
function callback($data, $delay) {
preg_match_all('/<h3>(.+)<//h3>/iU', $data, $matches);
usleep($delay);
return compact('data', 'matches');
}
數(shù)據(jù)處理回調(diào)無(wú)延遲時(shí): Rolling Curl略優(yōu), 但性能提升效果不明顯.
數(shù)據(jù)處理回調(diào)延遲5毫秒: Rolling Curl完勝, 性能提升40%左右.
通過上面的性能對(duì)比, 在處理URL隊(duì)列并發(fā)的應(yīng)用場(chǎng)景中Rolling cURL應(yīng)該是更加的選擇, 并發(fā)量非常大(1000+)時(shí), 可以控制并發(fā)隊(duì)列的最大長(zhǎng)度, 比如20, 每當(dāng)1個(gè)URL返回并處理完畢之后立即加入1個(gè)尚未請(qǐng)求的URL到隊(duì)列中, 這樣寫出來(lái)的代碼會(huì)更加健壯, 不至于并發(fā)數(shù)太大而卡死或崩潰. 詳細(xì)的實(shí)現(xiàn)請(qǐng)參考: http://code.google.com/p/rolling-curl/
php技術(shù):PHP curl 并發(fā)最佳實(shí)踐代碼分享,轉(zhuǎn)載需保留來(lái)源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請(qǐng)第一時(shí)間聯(lián)系我們修改或刪除,多謝。