Running a python program during idle time only

Mike Meyer mwm at mired.org
Sat May 21 01:04:14 EDT 2005


"los" <carlos80 at gmail.com> writes:
> I'm trying to create a program similar to that of Google's desktop that
> will crawl through the hard drive and index files.  I have written the
> program and as of now I just put the thread to sleep for 1 second after
> indexing a couple of files.
>
> I'm wondering if anyone knows of a way that I could make so that the
> program will run at full speed only runs after the computer has been
> idle for a while.  I've looked at the "nice" command but that's not
> exactly what I want.

On Unix, nice is exactly the answer. It's a lot more fine-grained than
what you're talking about, though. But it's the way things like
setiathome manage to run continuously without interfering with normal
usage.

If that's to fine grained for you, you could try waking up every few
minutes and checking the load average (via os.getloadavg), and only
doing work if the load average is low - say less than .5. While
running, you check the load average every couple of minutes and stop
if it rises noticably above 1 (you). The problem with this approach is
that it doesn't deal well with multiple tools doing this. I.e. - if
you run setiathome, your load average will always be above 1, so you
have to adjust your check value to take into account anything else
that might be in the background. Nice handles this automatically.

     <mike
-- 
Mike Meyer <mwm at mired.org>			http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.



More information about the Python-list mailing list