如果你用php进行并发请求,那么你可能会遇到没有数据返回的情况,这是本人在做并发请求遇到的实例。
官网的例子一般都会告诉你,multicurl可以实现并发网络访问,代码如下:
<?php
// create both cURL resources
$ch1 = curl_init();
$ch2 = curl_init();
// set URL and other appropriate options
curl_setopt($ch1, CURLOPT_URL, "http://lxr.php.net/");
curl_setopt($ch1, CURLOPT_HEADER, 0);
curl_setopt($ch2, CURLOPT_URL, "http://www.php.net/");
curl_setopt($ch2, CURLOPT_HEADER, 0);
//create the multiple cURL handle
$mh = curl_multi_init();
//add the two handles
curl_multi_add_handle($mh,$ch1);
curl_multi_add_handle($mh,$ch2);
$active = null;
//execute the handles
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
while ($active && $mrc == CURLM_OK) {
if (curl_multi_select($mh) != -1) {
do {
$mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);
}
}
//close the handles
curl_multi_remove_handle($mh, $ch1);
curl_multi_remove_handle($mh, $ch2);
curl_multi_close($mh);
?>
然后自己拿去测试,发现对呀,可以得到结果,然后大家都开始疯狂撰文说php的curl_multi ,实际上,经过我的时间经验,改函数对并发支持并发好,当我的并发数在一二十个的时候是正确的,但是再上去,就会有部分得不到数据,貌似已经到达该函数的上限了! 最终采用golang的goroutine来进行并发访问,几百个并发毫无压力!反过来说明php这个问题确实是存在的! 所以,大家在用它的时候注意了!