问题
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