Reduce server load / CPU .. sleep()?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tsg
Forum Contributor
Posts: 142
Joined: Sun Jan 12, 2003 9:22 pm
Location: SE, Alabama
Contact:

Reduce server load / CPU .. sleep()?

Post 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!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
tsg
Forum Contributor
Posts: 142
Joined: Sun Jan 12, 2003 9:22 pm
Location: SE, Alabama
Contact:

Post 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
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post 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.
tsg
Forum Contributor
Posts: 142
Joined: Sun Jan 12, 2003 9:22 pm
Location: SE, Alabama
Contact:

Post 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.
User avatar
AKA Panama Jack
Forum Regular
Posts: 878
Joined: Mon Nov 14, 2005 4:21 pm

Post 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.
tsg
Forum Contributor
Posts: 142
Joined: Sun Jan 12, 2003 9:22 pm
Location: SE, Alabama
Contact:

Post 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.
nincha
Forum Contributor
Posts: 191
Joined: Fri Mar 28, 2003 12:30 pm
Location: CA, USA

Post by nincha »

another suggestion is to use the http meta tag to refresh your browser, hense rerunning the script... it be just like sleeping.
tsg
Forum Contributor
Posts: 142
Joined: Sun Jan 12, 2003 9:22 pm
Location: SE, Alabama
Contact:

Post 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.
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post 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.
User avatar
Oren
DevNet Resident
Posts: 1640
Joined: Fri Apr 07, 2006 5:13 am
Location: Israel

Post 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)
tsg
Forum Contributor
Posts: 142
Joined: Sun Jan 12, 2003 9:22 pm
Location: SE, Alabama
Contact:

Post 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.
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post 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.
Post Reply