Page 1 of 1
Reduce server load / CPU .. sleep()?
Posted: Wed Aug 23, 2006 7:28 am
by tsg
I have a script I run that basically runs through a folder and generates thumbnails. Sometimes I can have hundred of images and can take a while to complete.
Occasionaly now it will stop and get a message on the screen from the hosting company saying I have gone over 20% CPU. (this is new)
So my question is, if I put a sleep() command in the script every so often, will that help reduce the CPU? Or will the script acuatlly still be running while it is sleeping and not make a difference.
Thanks!
Posted: Wed Aug 23, 2006 9:23 am
by feyd
It technically is still running, but it doesn't suck up CPU time that I remember. Another consideration is to use cron. Have the script process five or so images and then stop. Cron will hit it again and repeat. You'll have to tune how often cron will hit, but it's an option.
Another is downloading them all to your local machine and processing from there. The only other option is dependant on how the images get on the server. If they are uploaded via web interface, I'd create the thumbnail for each image at that time.
Posted: Wed Aug 23, 2006 9:46 am
by tsg
Thanks for the reply. Cron job not a viable option right now, even though probably a good way to go. I am going to work with the sleep option and see if it helps.
Thanks!
Tim
Posted: Wed Aug 23, 2006 10:23 am
by AKA Panama Jack
Many hosting companies have disabled the SLEEP function in PHP to prevent people from creating run away processes.
What you should be doing is creating your thumbnails AS SOON as the image is uploaded to the server. Waiting to create a thumbnail until a certain time of the day is highly impractical and wasteful of resources.
The other method is to create a thumbnail for an image at the time your site would normally display the thumbnail image if one doesn't exist.
Posted: Wed Aug 23, 2006 10:30 am
by tsg
Well I have one method where when images are upload via a form, it does create thumbnails then. This other method is where images are upload via ftp to the server, then my script processes them when I click a button.
This new CPU issue just came up on one hosting company, and not an issue on another. I am hoping the sleep will work (still testing a bunch) to reduce the CPU usage. I could do the thumbnails at the time of display, but that would slow things down on the customer side.
Posted: Wed Aug 23, 2006 10:40 am
by AKA Panama Jack
Well, if you absolutely have to process a bunch at a time what you can do is set your cron task to process say 10 images every half hour from your FTP upload.
When your cron executes it just processes the first 10 images it finds that do not have a thumbnail and then quits. Just have the cron task execute every 30 minutes. If all of the images have thumbnails it exits without doing anything. This would spread the load out.
Posted: Wed Aug 23, 2006 10:47 am
by tsg
That is pretty much what my script does now, finds images without thumbnails and creates them. Since the script itself will timeout after 30 seconds, I have a way around that also so it doesn't. So doing it with a cron wouldn't be real difficult.
So that leads to this question ... is there a way to create a cron job without having to set it like in cpanel?
but sometimes I have an event I have to get up pretty fast and the cron way would take a lot longer, but for events that can wait may be ok.
Posted: Wed Aug 23, 2006 10:53 am
by nincha
another suggestion is to use the http meta tag to refresh your browser, hense rerunning the script... it be just like sleeping.
Posted: Wed Aug 23, 2006 11:23 am
by tsg
nincha wrote:another suggestion is to use the http meta tag to refresh your browser, hense rerunning the script... it be just like sleeping.
I am using the meta refresh tag now to avoid the script time out.
Posted: Wed Aug 23, 2006 2:48 pm
by onion2k
Why don't you tell your hosting company not to change things without asking you about it first? You're paying them remember .. they work for you.
Posted: Wed Aug 23, 2006 2:51 pm
by Oren
tsg wrote:nincha wrote:another suggestion is to use the http meta tag to refresh your browser, hense rerunning the script... it be just like sleeping.
I am using the meta refresh tag now to avoid the script time out.
If this is a PHP script, just use
set_time_limit(0)
Posted: Wed Aug 23, 2006 2:53 pm
by tsg
Yea, for real. This isn't the first time things have changed without know.
But looks like my sleep() idea is working the way I want and is cutting down on the CPU. I have it running for 10 seconds, then sleeping for 10 seconds.
Posted: Wed Aug 23, 2006 3:22 pm
by Ollie Saunders
Occasionaly now it will stop and get a message on the screen from the hosting company saying I have gone over 20% CPU. (this is new)
That's terrible. They should cap your CPU usage and then only terminate the process when it stays at the peak of the cap for a very long time like 3 minutes.
Interesting thread though thanks for bringing this up tsg.