International PHP Conference Munich 2025

Voting

: min(six, eight)?
(Example: nine)

The Note You're Voting On

vigo dot von dot harrach at gmx dot de
13 years ago
curl_multi_select($mh, $timeout) simply blocks for $timeout seconds while curl_multi_exec() returns CURLM_CALL_MULTI_PERFORM. Otherwise, it works as intended, and blocks until at least one connection has completed or $timeout seconds, whatever happens first.

For that reason, curl_multi_exec() should always be wrapped:

<?php
function full_curl_multi_exec($mh, &$still_running) {
do {
$rv = curl_multi_exec($mh, $still_running);
} while (
$rv == CURLM_CALL_MULTI_PERFORM);
return
$rv;
}
?>

With that, the core of "multi" processing becomes (ignoring error handling for brevity):

<?php
full_curl_multi_exec
($mh, $still_running); // start requests
do { // "wait for completion"-loop
curl_multi_select($mh); // non-busy (!) wait for state change
full_curl_multi_exec($mh, $still_running); // get new state
while ($info = curl_multi_info_read($mh)) {
// process completed request (e.g. curl_multi_getcontent($info['handle']))
}
} while (
$still_running);
?>

Note that after starting requests, retrieval is done in the background - one of the better shots at parallel processing in PHP.

<< Back to user notes page

To Top