note 127124 added to function.curl-multi-exec
| From: | maxpanchnko at gmail dot com | Date: | Sun, 22 May 2022 12:15:31 +0000 |
| Subject: | note 127124 added to function.curl-multi-exec | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-218516@lists.php.net to get a copy of this message | ||
One of examples, how to make multi_curl faster twice (pseudocode) with Fibers:
<?php
$curlHandles = [];
$urls = [
'https://example.com/1',
'https://example.com/2',
...
'https://example.com/1000',
];
$mh = curl_multi_init();
$mh_fiber = curl_multi_init();
$halfOfList = floor(count($urls) / 2);
foreach ($urls as $index => $url) {
$ch = curl_init($url);
$curlHandles[] = $ch;
// half of urls will be run in background in fiber
$index > $halfOfList ? curl_multi_add_handle($mh_fiber, $ch) : curl_multi_add_handle($mh,
$ch);
}
$fiber = new Fiber(function (CurlMultiHandle $mh) {
$still_running = null;
do {
curl_multi_exec($mh, $still_running);
Fiber::suspend();
} while ($still_running);
});
// run curl multi exec in background while fiber is in suspend status
$fiber->start($mh_fiber);
$still_running = null;
do {
$status = curl_multi_exec($mh, $still_running);
} while ($still_running);
do {
/**
* at this moment curl in fiber already finished (maybe)
* so we must refresh $still_running variable with one more cycle "do while" in fiber
**/
$status_fiber = $fiber->resume();
} while (!$fiber->isTerminated());
foreach ($curlHandles as $index => $ch) {
$index > $halfOfList ? curl_multi_remove_handle($mh_fiber, $ch) :
curl_multi_remove_handle($mh, $ch);
}
curl_multi_close($mh);
curl_multi_close($mh_fiber);
?>
----
Server IP: 45.112.84.4
Probable Submitter: 185.85.3.47 (proxied: 86.40.72.94)
----
Manual Page -- https://php.net/manual/en/function.curl-multi-exec.php
Edit -- https://main.php.net/note/edit/127124
Del: integrated -- https://main.php.net/note/delete/127124/integrated
Del: useless -- https://main.php.net/note/delete/127124/useless
Del: bad code -- https://main.php.net/note/delete/127124/bad+code
Del: spam -- https://main.php.net/note/delete/127124/spam
Del: non-english -- https://main.php.net/note/delete/127124/non-english
Del: in docs -- https://main.php.net/note/delete/127124/in+docs
Del: other reasons-- https://main.php.net/note/delete/127124
Reject -- https://main.php.net/note/reject/127124
Search -- https://main.php.net/manage/user-notes.php