Jump to content

Recommended Posts

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);

You can not really "slow down" a script, but you can pause it for a while, by using usleep()

for example:

usleep(1000000);

 

stops the script for 1 sec, so if you would change it to usleep(50000) , it waits 50 milliseconds.

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

maybe it is me, but that loop look useless.

 

I am sure you have more code for it, and if so the problem might not be your loop processing too fast, but the amount of work the loop has to do and it may just be too much for your computer to handle.

The script is parsing data from 90 URL's it finishes in about 10 seconds but for a few seconds CPU is at 100% usage. Setting sleep or usleep(1) causes it to take 140 seconds to complete and thats way to long. My php.ini has a very high execution time, and 2 gigs of RAM but I have no memory issues just CPU. Some people in another forum suggested curl_multi_exec so going to try that.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.