Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

do while loop causing 100% CPU usage with curl_multi_exec

Tags:

loops

php

curl

cpu

This is a loop used in our script with curl. It's causing CPU usage to shoot up to 100%. A friend said "Your computer is looping so fast here it doesn't have time to process the request since it is constantly checking for a finish."... So my question is how can this loop be re-written to slow down? Thanks

$running = null;
do {
  curl_multi_exec($mh, $running);
} while($running > 0);
like image 335
Anagio Avatar asked Jun 21 '11 18:06

Anagio


3 Answers

Try this: http://php.net/manual/function.curl-multi-select.php

like image 119
Karoly Horvath Avatar answered Oct 29 '22 09:10

Karoly Horvath


Adding a call to http://php.net/sleep or http://php.net/usleep in every iteration should reduce the CPU usage by allowing other running processes to be scheduled by the operating system.

like image 44
webspy Avatar answered Oct 29 '22 10:10

webspy


Unfortunately you didn't post whole code. I suppose you are doing something like

$mh = curl_multi_init();
for ($i = 0; $i < $desiredThreadsNumber; $i++) {
    $ch = curl_init();
    // set up $ch here
    curl_multi_add_handle($mh, $ch);
}

You should understand that you haven't run threads yet here. curl_multi_exec() runs all threads. But it can't run all $desiredThreadsNumber threads simultaneously. If you look on example on curl_multi_exec() php.net page, you will see that you must wait while curl_multi_exec() run all threads. In other words, you need next nested loop here:

$running = null;
do {
    do {
        $mrc = curl_multi_exec($mh, $running);
    } while ($mrc == CURLM_CALL_MULTI_PERFORM);
} while($running > 0);

At the end let me suggest you to read this article http://www.onlineaspect.com/2009/01/26/how-to-use-curl_multi-without-blocking/ and use code snippet from there, I used it in 2 or 3 projects.

like image 24
Dmytro Zavalkin Avatar answered Oct 29 '22 10:10

Dmytro Zavalkin