PHP Multicurl高CPU使用率

时间:2018-04-18 09:57:19

标签: php curl

在我的1个vCPU VPS上以70 url运行此代码并且cpu跳到100%,这是正常的吗?

VPS - (LAMP) - PHP版本7.0.25,Intel Xeon CPU E5-2650 @ 2.00GHz

$curl_arr = array();
$master = curl_multi_init();

for($i = 0; $i < count($urls); $i++)
{
    $random = rand(1,9999);

    $link = trim($urls[$i]);

    $link = $link."?".$random;

    $url = trim($link);
    $curl_arr[$i] = curl_init($url);
    curl_setopt($curl_arr[$i], CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl_arr[$i], CURLOPT_FRESH_CONNECT, TRUE);
    curl_setopt($curl_arr[$i], CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36');
    curl_setopt($curl_arr[$i], CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl_arr[$i], CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl_arr[$i], CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl_arr[$i], CURLOPT_ENCODING , "gzip");
    curl_setopt($curl_arr[$i], CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4 );

    curl_multi_add_handle($master, $curl_arr[$i]);
}
do 
{
    curl_multi_exec($master,$running);
} 
while($running > 0);

1 个答案:

答案 0 :(得分:3)

是的,因为在您再次致电curl_multi_select()之前,当您未使用curl_multi_exec()等待活动时,这正是您所要求的......

这个简单的改变应该可以更好地发挥作用:

do {
   curl_multi_select($master);
   curl_multi_exec($master, $running);
} while ($running);