简体   繁体   中英

Is curl_multi_exec() a blocking call?

如果PHP中的curl_multi_exec()调用是阻塞或非阻塞调用,那只是好奇。

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM