Is curl_multi_exec() a blocking call?

戏子无情 提交于 2019-12-23 09:56:27

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/14391368/is-curl-multi-exec-a-blocking-call

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!