Solve the problem of high CPU load caused by PHP curl_multi batch processing

Source: Internet
Author: User
Tags foreach curl

The simple curl process is as follows:

The code is as follows Copy Code

$ch = Curl_init ();
curl_setopt ($ch, Curlopt_url, ' ');
curl_setopt ($ch, Curlopt_returntransfer, TRUE);
$con = curl_exec ($ch);
Curl_close ($ch);

Curl also provides a bulk processing session, and the following is the Curl batch processing correlation function:
curl_multi_init-returns a new Curl batch handle
curl_multi_add_handle-Add a separate curl handle to a curl batch session
curl_multi_exec-resolves a curl batch handle
curl_multi_getcontent-If the Curlopt_returntransfer is set, the text stream of the obtained output is returned
curl_multi_select-waits for active connections in all Curl batches
Curl_multi_info_read-gets the relevant transport information of the currently resolved curl
curl_multi_remove_handle-removes a handle resource from a curl batch handle resource
Curl_multi_close-closes a set of curl handles

Look at the following examples of using Curl Multi batch processing:

The code is as follows Copy Code
* CURL Multi Batch Processing
* @author McKee
* @link

$url _array = Array (
' Http:// ',
' Http:// '

$handles = $contents = Array ();

Initializing Curl Multi Objects
$MH = Curl_multi_init ();

Add Curl Batch Session
foreach ($url _array as $key => $url)
$handles [$key] = Curl_init ($url);
curl_setopt ($handles [$key], Curlopt_returntransfer, 1);
curl_setopt ($handles [$key], curlopt_timeout, 10);

Curl_multi_add_handle ($MH, $handles [$key]);

====================== Execute Batch handle =================================
$active = null;
do {
$MRC = Curl_multi_exec ($MH, $active);
while ($MRC = = Curlm_call_multi_perform);

while ($active and $MRC = = CURLM_OK) {

if (Curl_multi_select ($mh) = = 1) {
Usleep (100);
do {
$MRC = Curl_multi_exec ($MH, $active);
while ($MRC = = Curlm_call_multi_perform);


Get Batch Content
foreach ($handles as $i => $ch)
$content = Curl_multi_getcontent ($ch);
$contents [$i] = Curl_errno ($ch) = = 0? $content: ';

To remove a batch handle
foreach ($handles as $ch)
Curl_multi_remove_handle ($MH, $ch);

Close Batch Handle
Curl_multi_close ($MH);

Print_r ($contents);

The above procedure focuses on the implementation of the batch process, the normal processing:

The code is as follows Copy Code
do {$n =curl_multi_exec ($MH, $active);} while ($active);


will cause the CPU loading too high, because $active to wait for all the URL data accepted to become false, so here to use the return value of curl_multi_exec to determine whether there is data, when there is time to call Curl_multi_ exec, you sleep without performing data, so you avoid CPU Loading 100%.

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: 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.