Running a python program during idle time only

James Carroll mrmaple at gmail.com
Sat May 21 15:49:32 EDT 2005


There's probably a way to tell how long the user has been idle, but here's
how you can check the CPU load to see if it's elevated... (of course
your program might elevate  it too.)

On linux, you can read from /proc/loadavg 

Here's a fun thing to try on windows...

make sure you have the win32all package installed...

import win32com.client
computer="."
query="select LoadPercentage from Win32_Processor where DeviceID = 'CPU0'"
objWMIService = win32com.client.Dispatch("WbemScripting.SWbemLocator")
objSWbemServices = objWMIService.ConnectServer(computer,"root\cimv2")
rows = objSWbemServices.ExecQuery(query)
print rows[0].LoadPercentage

import time

for count in xrange(4):
    print objSWbemServices.ExecQuery(query)[0].LoadPercentage
    time.sleep(1)

The microsoft docs on the info you can get on the processor is here:
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/win32_processor.asp

If you run this from a shell like PyCrust, you won't see anything for
four seconds, and then it will all show up.

What are you using for the text indexing?  PyLucene???  

-Jim

On 5/21/05, Donn Cave <donn at drizzle.com> wrote:
> Quoth Mike Meyer <mwm at mired.org>:
> | "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.
> 
> Well, he could certainly try it if he hasn't already, but I wouldn't
> be too surprised if it really isn't exactly what he wants.  Maybe it
> depends on the scheduler, and maybe things have gotten a lot better
> in that department, but from my experience a process can be niced pretty
> hard and still tie up a lot of resources.  A disk crawler sounds like
> a good example.
> 
> I don't have any brilliant ideas, though.  Your suggestion to use
> os.getloadavg sounds good to me.  It might be kind of neat if there
> were some kind of APM ioctl that would register the calling process
> for notification of pending disk spin down, low power mode etc.
> 
>         Donn
> --
> http://mail.python.org/mailman/listinfo/python-list
> 
>



More information about the Python-list mailing list