Anag Posted June 21, 2011 Share Posted June 21, 2011 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); Quote Link to comment https://forums.phpfreaks.com/topic/240023-curl-do-while-loop-causing-100-cpu-usage/ Share on other sites More sharing options...
maximeke2 Posted June 21, 2011 Share Posted June 21, 2011 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); Quote Link to comment https://forums.phpfreaks.com/topic/240023-curl-do-while-loop-causing-100-cpu-usage/#findComment-1232975 Share on other sites More sharing options...
The Little Guy Posted June 21, 2011 Share Posted June 21, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/240023-curl-do-while-loop-causing-100-cpu-usage/#findComment-1233044 Share on other sites More sharing options...
Anag Posted June 21, 2011 Author Share Posted June 21, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/240023-curl-do-while-loop-causing-100-cpu-usage/#findComment-1233049 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.