Home > php教程 > php手册 > body text

解决php curl_multi批处理造成CPU负载过高问题

WBOY
Release: 2016-05-25 16:40:33
Original
2608 people have browsed it

今天在利用curl_multi函数来获取一些外网内容时发现只要一运行curl_multi函数我的cpu就占得非常的高,后来看一站长分享了此问题解决方法我也整理一下与各位分享一下,希望对大家有帮助.

简单的cURL处理如下,代码如下:

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, 'http://www.phprm.com'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); 
$con = curl_exec($ch); 
curl_close($ch);
Copy after login

cURL还提供了批量处理会话,下面是cURL批量处理相关函数:

curl_multi_init — 返回一个新cURL批处理句柄

curl_multi_add_handle — 向curl批处理会话中添加单独的curl句柄

curl_multi_exec — 解析一个cURL批处理句柄

curl_multi_getcontent — 如果设置了CURLOPT_RETURNTRANSFER,则返回获取的输出的文本流

curl_multi_select — 等待所有cURL批处理中的活动连接

curl_multi_info_read — 获取当前解析的cURL的相关传输信息

curl_multi_remove_handle — 移除curl批处理句柄资源中的某个句柄资源

curl_multi_close — 关闭一组cURL句柄

看下面使用curl multi批处理的例子,代码如下:

/** 
 * cURL multi批量处理 
 *  
 * @author mckee 
 * @link http://www.phprm.com 
 *  
 */ 
 
$url_array = array( 
    'http://www.phpfensi.net/', 
    'http://www.phpfensi.net/php/627.html', 
    'http://www.phpfensi.net/php/258.html' 
); 
 
$handles = $contents = array();  
 
//初始化curl multi对象 
$mh = curl_multi_init(); 
 
//添加curl 批处理会话 
foreach($url_array as $key => $url) 
{ 
    $handles[$key] = curl_init($url); 
    curl_setopt($handles[$key], CURLOPT_RETURNTRANSFER, 1); 
    curl_setopt($handles[$key], CURLOPT_TIMEOUT, 10); 
     
    curl_multi_add_handle($mh, $handles[$key]); 
}//开源代码phprm.com 
 
//======================执行批处理句柄================================= 
$active = null; 
do { 
    $mrc = curl_multi_exec($mh, $active); 
} while ($mrc == CURLM_CALL_MULTI_PERFORM); 
 
 
while ($active and $mrc == CURLM_OK) { 
     
    if(curl_multi_select($mh) === -1){ 
        usleep(100); 
    } 
    do { 
        $mrc = curl_multi_exec($mh, $active); 
    } while ($mrc == CURLM_CALL_MULTI_PERFORM); 
 
} 
//==================================================================== 
 
//获取批处理内容 
foreach($handles as $i => $ch) 
{ 
    $content = curl_multi_getcontent($ch); 
    $contents[$i] = curl_errno($ch) == 0 ? $content : ''; 
} 
 
//移除批处理句柄 
foreach($handles as $ch) 
{ 
    curl_multi_remove_handle($mh, $ch); 
} 
 
//关闭批处理句柄 
curl_multi_close($mh); 
 
print_r($contents);
Copy after login

上面这段程序重点是执行批处理的那段,普通的处理,代码如下:

do { $n=curl_multi_exec($mh,$active); } while ($active);
Copy after login

会造成CPU Loading过高,因为$active要等全部url数据接受完毕才变成false,所以这里用到了curl_multi_exec的返回值判断是否还有数据,当有数据的时候就不停调用curl_multi_exec,没有执行数据就会sleep,如此就会避免CPU Loading 100%了.


永久链接:

转载随意!带上文章地址吧。

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template