Reduce server load / CPU .. sleep()?
Moderator: General Moderators
Reduce server load / CPU .. sleep()?
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!
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!
- feyd
- Neighborhood Spidermoddy
- Posts: 31559
- Joined: Mon Mar 29, 2004 3:24 pm
- Location: Bothell, Washington, USA
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.
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.
- AKA Panama Jack
- Forum Regular
- Posts: 878
- Joined: Mon Nov 14, 2005 4:21 pm
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.
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.
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.
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.
- AKA Panama Jack
- Forum Regular
- Posts: 878
- Joined: Mon Nov 14, 2005 4:21 pm
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.
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.
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.
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.
If this is a PHP script, just use set_time_limit(0)tsg wrote:I am using the meta refresh tag now to avoid the script time out.nincha wrote:another suggestion is to use the http meta tag to refresh your browser, hense rerunning the script... it be just like sleeping.
- Ollie Saunders
- DevNet Master
- Posts: 3179
- Joined: Tue May 24, 2005 6:01 pm
- Location: UK
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.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)
Interesting thread though thanks for bringing this up tsg.