使用多线程cURL时发现的一个问题

  当PHP使用多线程版本的cURL时能够提升不少效率,可是按照不少地方都给出了这个例子(http://cn2.php.net/manual/zh/function.curl-multi-exec.phpphp

 1 <?php
 2 // 建立一对cURL资源
 3 $ch1 = curl_init();
 4 $ch2 = curl_init();
 5 
 6 // 设置URL和相应的选项
 7 curl_setopt($ch1, CURLOPT_URL, "http://lxr.php.net/");
 8 curl_setopt($ch1, CURLOPT_HEADER, 0);
 9 curl_setopt($ch2, CURLOPT_URL, "http://www.php.net/");
10 curl_setopt($ch2, CURLOPT_HEADER, 0);
11 
12 // 建立批处理cURL句柄
13 $mh = curl_multi_init();
14 
15 // 增长2个句柄
16 curl_multi_add_handle($mh,$ch1);
17 curl_multi_add_handle($mh,$ch2);
18 
19 $active = null;
20 // 执行批处理句柄
21 do {
22     $mrc = curl_multi_exec($mh, $active);
23 } while ($mrc == CURLM_CALL_MULTI_PERFORM);
24 
25 while ($active && $mrc == CURLM_OK) {
26     if (curl_multi_select($mh) != -1) { 27         do {
28             $mrc = curl_multi_exec($mh, $active);
29         } while ($mrc == CURLM_CALL_MULTI_PERFORM);
30  } 31 }
32 
33 // 关闭所有句柄
34 curl_multi_remove_handle($mh, $ch1);
35 curl_multi_remove_handle($mh, $ch2);
36 curl_multi_close($mh);
37 
38 ?>

 

  须要注意的是第26行代码,在个人机器环境下(PHP 5.3.13),curl_multi_select函数会一直返回-1,造成成死循环,去掉就行了。多线程

相关文章
相关标签/搜索