why curl_multi_exec in two loops

2019-09-04 07:04发布

问题:

I saw a piece of example code, i wonder why use two do-while loops? what are difference between the two loops? wait reply online, Thank You~~

do {
    $mrc = curl_multi_exec($mh, $active);
} while ($mrc == CURLM_CALL_MULTI_PERFORM);

while ($active && $mrc == CURLM_OK) {
    if (curl_multi_select($mh) != -1) {
        do {
            $mrc = curl_multi_exec($mh, $active);
        } while ($mrc == CURLM_CALL_MULTI_PERFORM);
    }
}

回答1:

As presented, the first loop is intended to initialize the HTTP clients. Normally it only executes once. Then in the second loop the HTTP requests are sent and the responses reaped.

This isn't very handy if you want your script to do something while its waiting for the HTTP requests to be handled (you could put some of the stuff you want to do in a separate page and call that as a curl resource - but its a bit messy).

See this page for more details and alternate constructs.