tags:

views:

2178

answers:

5

I have a "generate website" command, that parses through all the tables to republish an entire website into fixed html pages. That's a heavy process, at least on my local machine (the CPU rises up). On the production server it does not seem to be a problem so far but i would like to keep it future proof. Therefore i'm considering using the php sleep() function between each step of the heavy script so that the server has time to "catch its breath" between the heavy steps.

Is that a good idea or will it be useless?

A: 

I think it is a good idea. Sleep means a repititive comparison of ticks until a period occurs. The overhead on CPU on a sleep operation should be lower.

Chathuranga Chandrasekara
+3  A: 

If you're running php5, and it's being used in CGI (rather than mod_php) mode, then you could consider using proc_nice instead.

This could allow the "generate website" command to use as much CPU as it wants while no-one else is trying to use the site.

Alnitak
A: 

It depends on how many times you'll be calling it and for how long. You'll need to balance your need to a quick output vs. low CPU usage.

In short: yes, it'll help.

Seb
it will be called 6 times, 3 second each. I don't need quick output at all. I'm thinking of starting it via an ajax call, then have another ajaxcall retrieve the status every 19 seconds. it should only say "html files generated" or output a log if an error occured.
pixeline
A: 

I would simply not do this on the Production Server, the steps I have followed before:

  1. Rent a low cost PHP server - or get a proper Dev server setup that replicates the production

  2. All Dynamic files are copied to DEV - they dont even need to be on the production

  3. Run the HTMLizer script - no sleep just burn it out

  4. Validate the ouput and then RSYNC this to the live server - backing up the live directory as you do it so you can safely fall back

Anyway since Caching / Memcaching came up to speed I havent had to do this at all - use Zend Framework and Zend Cache and you basically have a dynamic equivalent to what you need to do automatically.

Ian Warner
the method you propose sounds good. care to elaborate on RSYNC? about zen: i may be wrong (i don't know zen), but I cannot use zend cache because i load these generated html fragments via ajax through a fixed html file name. So i need to control the exact position and file name of the html files.
pixeline
RSYNC http://samba.anu.edu.au/rsync/rsync is an open source utility that provides fast incremental file transferZend is a PHP Framework - but there are plenty of Caching classes - i.e. in PEAR.
Ian Warner
A: 

Based on the task, I don't think it will help you.

Sleep would only really be useful if you were continuously looping and waiting for user input or trigger signal.

In this case, to get the job done ASAP you may as well omit the sleep command, thus reducing the task time and freeing up the CPU time faster for other processes.

Some of the posters above may be able to better assist you with code optimisation.