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

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

 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,形成成死循环,去掉就好了。

posted @ 2014-02-10 15:21  xupengzhuo  阅读(4137)  评论(0编辑  收藏  举报