Home > Backend Development > PHP Tutorial > php curl批处理-可控并发异步

php curl批处理-可控并发异步

WBOY
Release: 2016-06-13 10:49:46
Original
1005 people have browsed it

php curl批处理--可控并发异步
在php的手册里面有一段代码:

$mrc = curl_multi_init();//发出请求.......$active = null;		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);		    }		}//下面是处理请求返回的结果
Copy after login


这段do...while是用来控制并发的,效果是将$mrc所有的请求完毕,再执行response结果

但如果我有1000个请求,那么curl批处理将并发1000个请求,显然是不合理,所以应该要控制一个并发数,并且将剩余的连接添加到请求队列里:
参考:http://www.onlineaspect.com/2009/01/26/how-to-use-curl_multi-without-blocking/

  $mh = curl_multi_init();        $ch = array();        $chunck = 10; //并发控制数        $all = count($urls);//所有的请求url数组        $chunck = $all > $chunck ? $chunck : $all;				$options = array(			CURLOPT_HEADER=>FALSE,			CURLOPT_RETURNTRANSFER=>TRUE,			CURLOPT_FOLLOWLOCATION=>TRUE,			CURLOPT_MAXREDIRS=>5,			CURLOPT_USERAGENT=>'Mozilla/5.0 (Windows NT 6.1; rv:6.0) Gecko/20100101 Firefox/6.0'		);				for($i = 0 ; $i <br><br>使用下来效果很好,没有副作用,并发数可控,应用之处多多,自己发挥想象吧<div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template