note 127125 added to language.fibers

From: Date: Sun, 22 May 2022 12:16:37 +0000
Subject: note 127125 added to language.fibers
Groups: php.notes 
Request: Send a blank email to php-notes+get-218517@lists.php.net to get a copy of this message
One of examples, how to make multi_curl faster twice (pseudocode) using 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/language.fibers.php Edit -- https://main.php.net/note/edit/127125 Del: integrated -- https://main.php.net/note/delete/127125/integrated Del: useless -- https://main.php.net/note/delete/127125/useless Del: bad code -- https://main.php.net/note/delete/127125/bad+code Del: spam -- https://main.php.net/note/delete/127125/spam Del: non-english -- https://main.php.net/note/delete/127125/non-english Del: in docs -- https://main.php.net/note/delete/127125/in+docs Del: other reasons-- https://main.php.net/note/delete/127125 Reject -- https://main.php.net/note/reject/127125 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#218517) next »