Skip to content

Commit de0c528

Browse files
author
XieBiao
committed
improve multi select
1 parent f75d9e4 commit de0c528

File tree

1 file changed

+19
-20
lines changed

1 file changed

+19
-20
lines changed

src/MultiCurl.php

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,33 +32,32 @@ public function addCurls(array $curls)
3232
return true;
3333
}
3434

35-
public function exec()
35+
public function exec($selectTimeout = 1.0)
3636
{
3737
if (count($this->curls) == 0) {
3838
return false;
3939
}
4040

41-
$running = null;
42-
do {
43-
usleep(10);
44-
curl_multi_exec($this->handle, $running);
45-
} while ($running > 0);
41+
// $running = null;
42+
// do {
43+
// usleep(100);
44+
// curl_multi_exec($this->handle, $running);
45+
// } while ($running > 0);
4646

4747

48-
// $active = null;
49-
// do {
50-
// $mrc = curl_multi_exec($this->handle, $active);
51-
// usleep(10);
52-
// } while ($mrc == CURLM_CALL_MULTI_PERFORM);
53-
//
54-
// while ($active && $mrc == CURLM_OK) {
55-
// if (curl_multi_select($this->handle) != -1) {
56-
// do {
57-
// $mrc = curl_multi_exec($this->handle, $active);
58-
// } while ($mrc == CURLM_CALL_MULTI_PERFORM);
59-
// }
60-
// usleep(10);
61-
// }
48+
// The first curl_multi_select often times out no matter what, but is usually required for fast transfers
49+
$timeout = 0.001;
50+
$active = false;
51+
do {
52+
while (($mrc = curl_multi_exec($this->handle, $active)) == CURLM_CALL_MULTI_PERFORM) {
53+
;
54+
}
55+
if ($active && curl_multi_select($this->handle, $timeout) === -1) {
56+
// Perform a usleep if a select returns -1: https://bugs.php.net/bug.php?id=61141
57+
usleep(150);
58+
}
59+
$timeout = $selectTimeout;
60+
} while ($active);
6261

6362
$this->clean();
6463
}

0 commit comments

Comments
 (0)