|
在實際項目或者自己編寫小工具(比如新聞聚合,商品價格監(jiān)控,比價)的過程中, 通常需要從第3方網(wǎng)站或者API接口獲取數(shù)據(jù), 在需要處理1個URL隊列時, 為了提高性能, 可以采用cURL提供的curl_multi_*族函數(shù)實現(xiàn)簡單的并發(fā)。
本文將探討兩種具體的實現(xiàn)方法, 并對不同的方法做簡單的性能對比.
1. 經(jīng)典cURL并發(fā)機制及其存在的問題
經(jīng)典的cURL實現(xiàn)機制在網(wǎng)上很容易找到, 比如參考php在線手冊的如下實現(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ā)隊列, 然后執(zhí)行并發(fā)過程, 等待所有請求接收完之后進行數(shù)據(jù)的解析等后續(xù)處理. 在實際的處理過程中, 受網(wǎng)絡(luò)傳輸?shù)挠绊? 部分URL的內(nèi)容會優(yōu)先于其他URL返回, 但是經(jīng)典cURL并發(fā)必須等待最慢的那個URL返回之后才開始處理, 等待也就意味著CPU的空閑和浪費. 如果URL隊列很短, 這種空閑和浪費還處在可接受的范圍, 但如果隊列很長, 這種等待和浪費將變得不可接受.
2. 改進的Rolling cURL并發(fā)方式
仔細分析不難發(fā)現(xiàn)經(jīng)典cURL并發(fā)還存在優(yōu)化的空間, 優(yōu)化的方式時當(dāng)某個URL請求完畢之后盡可能快的去處理它, 邊處理邊等待其他的URL返回, 而不是等待那個最慢的接口返回之后才開始處理等工作, 從而避免CPU的空閑和浪費. 閑話不多說, 下面貼上具體的實現(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ā)實現(xiàn)的性能對比
改進前后的性能對比試驗在LINUX主機上進行, 測試時使用的并發(fā)隊列如下:
http://a.com/item.htm?id=14392877692
http:/a.com/item.htm?id=16231676302
http://a.com/item.htm?id=5522416710
http://a.com/item.htm?id=16551116403
簡要說明下實驗設(shè)計的原則和性能測試結(jié)果的格式: 為保證結(jié)果的可靠, 每組實驗重復(fù)20次, 在單次實驗中, 給定相同的接口URL集合, 分別測量Classic(指經(jīng)典的并發(fā)機制)和Rolling(指改進后的并發(fā)機制)兩種并發(fā)機制的耗時(秒為單位), 耗時短者勝出(Winner), 并計算節(jié)省的時間(Excellence, 秒為單位)以及性能提升比例(Excel. %). 為了盡量貼近真實的請求而又保持實驗的簡單, 在對返回結(jié)果的處理上只是做了簡單的正則表達式匹配, 而沒有進行其他復(fù)雜的操作. 另外, 為了確定結(jié)果處理回調(diào)對性能對比測試結(jié)果的影響, 可以使用usleep模擬現(xiàn)實中比較負責(zé)的數(shù)據(jù)處理邏輯(如提取, 分詞, 寫入文件或數(shù)據(jù)庫等).
性能測試中用到的回調(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)無延遲時: Rolling Curl略優(yōu), 但性能提升效果不明顯。
php技術(shù):php cURL和Rolling cURL并發(fā)方式比較,轉(zhuǎn)載需保留來源!
鄭重聲明:本文版權(quán)歸原作者所有,轉(zhuǎn)載文章僅為傳播更多信息之目的,如作者信息標(biāo)記有誤,請第一時間聯(lián)系我們修改或刪除,多謝。