Batch processing of curl characteristics

Source: Internet
Author: User
Tags curl

Curl Batch processing

Curl also has an advanced feature- batch handle (handle). This feature allows multiple curl connections to be opened simultaneously or asynchronously.

"Sample":

<span style= "FONT-FAMILY:FANGSONG_GB2312;FONT-SIZE:18PX;" ><?
      PHP//Create two curl resources $ch 1 = curl_init ();
      $ch 2 = Curl_init ();
      Specifies the URL and the appropriate parameter curl_setopt ($ch 1,curlopt_url, "http://lxr.php.net/");
      curl_setopt ($ch 1,curlopt_header,0);
      curl_setopt ($ch 2,curlopt_url, "http://www.php.net/");
      curl_setopt ($ch 2,curlopt_header,0);
      Create Curl Batch Handle $MH = Curl_multi_init ();
      Add a handle to the previous two resources curl_multi_add_handle ($MH, $ch 1);
      Curl_multi_add_handle ($MH, $ch 2);
      Predefined one state variable $active = NULL;
      Execute batch processing 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 each handle Curl_multi_remove_handle ($MH, $ch 1); Curl_multi_remove_handle ($MH, $ch 2); Curl_multi_close ($MH);?></span>


"Resolution":

The thing to do here is to open multiple curl handles and assign them to a batch handle, and then just wait in a while loop to complete the execution.

The first do ... while loop repeats the invocation of Curl_multi_exec (). This function is not partition-free. But will do as little as possible. It returns a state value, as long as the value equals constant curm_call_multi_perform, there is still work to be done. (for example, send the HTTP header information of the corresponding URL), that is, the function needs to be called continuously until the return value changes.

The next while loop, which is passed to Curl_multi_exec () as the second argument before continuing the variable when the $active variable is true, represents whether there are active connections in the batch handle as well. Then call Curl_multi_select (), which is blocked until the active connection appears. Once this function succeeds, it goes into another do...while loop and continues to the next URL.

"description":

Many people call this approach curl multithreading, and curl_multi_exec is not a multithreaded, it belongs to the category of asynchronous processing.

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.