Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is curl_multi_exec() a blocking call?

Was just curious if the curl_multi_exec() call in PHP is blocking or non-blocking call.

like image 535
Adobri Avatar asked Jan 18 '13 02:01

Adobri


1 Answers

Shot answer : curl_multi_exec() is non-blocking


Longer answer : curl_multi_exec() is non-blocking, but blocking can be made with the combination of curl_multi_select, which blocks until there is activity on any of the curl_multi connections.

Edit: Currently I am working on a crawler, this is outline of a piece of code I used.

do {
    $mrc = curl_multi_exec($mh, $active);
    if($to_db_queue->count()>0){
       while($to_db_queue->count()>0)
          //dequeue from queue and insert into database
    }
    else  
      curl_multi_select($mh); //block till state change
} while ($active > 0);

This code will make a curl_multi_exec and then will continue its database work queued in $to_db_queue, else if nothing in queue curl_multi_select will be called to block the loop until a state change occur in curl_multi connections.

More example:
non-blocking
blocking

Hope this will help you understand the concept.

like image 181
Cyril Avatar answered Oct 11 '22 02:10

Cyril