Processing network data using Curl_multi_init parallel multithreading

Source: Internet
Author: User
 for($i=0;$i< -;$i++){$urls[] ="http://blog.csdn.net/lg_lin?wd=". Mt_rand (10000,20000);}$t= Microtime (true);//Single threadforeach($urls as$key=$value){$ch= Curl_init (); curl_setopt ($ch, Curlopt_ssl_verifypeer,FALSE); curl_setopt ($ch, Curlopt_returntransfer,TRUE); curl_setopt ($ch, Curlopt_timeout, -); curl_setopt ($ch, Curlopt_url,$value);$response= Curl_exec ($ch); Curl_close ($ch);$result _new[$key] =$response;} Var_dump ($result _new);$e= Microtime (true);Echo"For loop:". ($e-$t)."\ n";/** * * * * * * * * * due to network bandwidth constraints, one-time can not concurrency too much, batch processing * Total more than 100, the effect is significantly higher than single-thread * *$p=5;//How many bars per execution$ring= Ceil (COUNT ($urls) /$p); for($n=0;$n<$ring;$n++){$temp _url=Array();$star=$n*$p;$end= ($n+1) *$p; for($ii=$star;$ii<$end;$ii++)    {if(isset($urls[$ii]))        {$temp _url[] =$urls[$ii]; }    }$MH= Curl_multi_init ();foreach($temp _url as$i=$url) {$conn[$i] = Curl_init ($url); curl_setopt ($conn[$i], Curlopt_useragent,$_server[' Http_user_agent ']); curl_setopt ($conn[$i], Curlopt_ssl_verifypeer,FALSE); curl_setopt ($conn[$i], Curlopt_returntransfer,TRUE); curl_setopt ($conn[$i], Curlopt_timeout, -); curl_setopt ($conn[$i], Curlopt_url,$url); Curl_multi_add_handle ($MH,$conn[$i]); } Do{Curl_multi_exec ($MH,$active); } while($active);$active=NULL;foreach($temp _url as$i=$url) {$data[$i] = Curl_multi_getcontent ($conn[$i]);//Get the code string for the crawl}foreach($temp _url as$i=$url) {Curl_multi_remove_handle ($MH,$conn[$i]); Curl_close ($conn[$i]); } curl_multi_close ($MH); Var_dump ($data);}$t= Microtime (true);Echo"Multithreading:". ($t-$e)."\ n";Exit();

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The above describes the use of Curl_multi_init parallel multithreading processing network data, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.