PHP curl 并發(fā)最佳實踐代碼分享
更新時間:2012年09月05日 23:13:26 作者:
在實際項目或者自己編寫小工具(比如新聞聚合,商品價格監(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)方式:
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)化的方式時當某個URL請求完畢之后盡可能快的去處理它, 邊處理邊等待其他的URL返回, 而不是等待那個最慢的接口返回之后才開始處理等工作, 從而避免CPU的空閑和浪費. 閑話不多說, 下面貼上具體的實現(xiàn):
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://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
簡要說明下實驗設(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ù)為:
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), 但性能提升效果不明顯.
數(shù)據(jù)處理回調(diào)延遲5毫秒: Rolling Curl完勝, 性能提升40%左右.
通過上面的性能對比, 在處理URL隊列并發(fā)的應(yīng)用場景中Rolling cURL應(yīng)該是更加的選擇, 并發(fā)量非常大(1000+)時, 可以控制并發(fā)隊列的最大長度, 比如20, 每當1個URL返回并處理完畢之后立即加入1個尚未請求的URL到隊列中, 這樣寫出來的代碼會更加健壯, 不至于并發(fā)數(shù)太大而卡死或崩潰. 詳細的實現(xiàn)請參考: http://code.google.com/p/rolling-curl/
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)化的方式時當某個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://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
簡要說明下實驗設(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), 但性能提升效果不明顯.
數(shù)據(jù)處理回調(diào)延遲5毫秒: Rolling Curl完勝, 性能提升40%左右.
通過上面的性能對比, 在處理URL隊列并發(fā)的應(yīng)用場景中Rolling cURL應(yīng)該是更加的選擇, 并發(fā)量非常大(1000+)時, 可以控制并發(fā)隊列的最大長度, 比如20, 每當1個URL返回并處理完畢之后立即加入1個尚未請求的URL到隊列中, 這樣寫出來的代碼會更加健壯, 不至于并發(fā)數(shù)太大而卡死或崩潰. 詳細的實現(xiàn)請參考: http://code.google.com/p/rolling-curl/
您可能感興趣的文章:
- php curl批處理實現(xiàn)可控并發(fā)異步操作示例
- php使用curl并發(fā)減少后端訪問時間的方法分析
- php cURL和Rolling cURL并發(fā)方式比較
- PHP使用curl_multi實現(xiàn)并發(fā)請求的方法示例
- PHP中使用cURL實現(xiàn)Get和Post請求的方法
- php之curl實現(xiàn)http與https請求的方法
- php curl模擬post請求和提交多維數(shù)組的示例代碼
- php curl請求信息和返回信息設(shè)置代碼實例
- php curl 獲取https請求的2種方法
- PHP curl批處理及多請求并發(fā)實現(xiàn)方法分析
相關(guān)文章
PHP實現(xiàn)的常規(guī)正則驗證helper公共類完整實例
這篇文章主要介紹了PHP實現(xiàn)的常規(guī)正則驗證helper公共類,結(jié)合完整實例形式分析了php針對常規(guī)的電話、手機、郵箱、賬號等進行正則驗證的操作技巧,需要的朋友可以參考下2017-04-04PHP保存Base64圖片base64_decode的問題整理
在本篇文章里小編給大家整理的是關(guān)于PHP保存Base64圖片base64_decode的問題,需要的朋友們參考下。2019-11-11