note 108928 modified in function.curl-multi-select by danbrown
| From: | danbrown@php.net | Date: | Wed, 06 Jun 2012 17:54:58 +0000 |
| Subject: | note 108928 modified in function.curl-multi-select by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-188431@lists.php.net to get a copy of this message | ||
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.
--was--
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:
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):
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.
http://php.net/manual/en/function.curl-multi-select.php