昨天在对php网站进行性能优化时,采用了多线程进行网络请求的方法,用官网的例子:
<span style="font-size:14px;">// build the individual requests, but do not execute them
$ch_1 = curl_init('http://webservice.one.com/');
$ch_2 = curl_init('http://webservice.two.com/');
curl_setopt($ch_1, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch_2, CURLOPT_RETURNTRANSFER, true);
// build the multi-curl handle, adding both $ch
$mh = curl_multi_init();
curl_multi_add_handle($mh, $ch_1);
curl_multi_add_handle($mh, $ch_2);
// execute all queries simultaneously, and continue when all are complete
$running = null;
do {
curl_multi_exec($mh, $running);
} while ($running);
//close the handles
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_multi_close($mh);
// all of our requests are done, we can now access the results
$response_1 = curl_multi_getcontent($ch_1);
$response_2 = curl_multi_getcontent($ch_2);
echo "$response_1 $response_2"; // output results</span>
结果在本地max上执行时okay的,放到linux上出错了,报错误:
<span style="font-size:14px;">Maximum execution time of 30 seconds exceeded curl_multi_select</span>
后来在stackoverflow上找到答案,说将:$mrc==CURLM_CALL_MULTI_PERFORM 这个条件改成$active>0 ;如果不行就改成 :$mrc==CURLM_CALL_MULTI_PERFORM,
因此,最终我的判断条件改为: $active>0 || $mrc == CURLM_CALL_MULTI_PERFORM ,现在两个机器上都okay了!