detecting idle processor

Gerhard Häring gerhard.nospam at bigfoot.de
Mon Jun 11 02:41:57 EDT 2001


On 11 Jun 2001 00:00:41 -0700, Sorin Gherman <s_gherman at yahoo.com> wrote:
>How can I use Python to detect when the processor is idle? Or when the network
>traffic is idle, for a given computer?
>
>I need this to implement a background program.

This is depends very much on the operating system. For the first question
(processor is idle), I think the answer is that you can't do it. All you can do
is run the Python program with a very low priority. This means that it won't
steal much CPU from processes that run at a higher priority. On Unix, you'll
want to use nice for this.

On my Linux system, 

nice --adjustment=19 myscript.py

does just that. The parameters of nice would be different on other Unixen, of
course.

Measuring network load is more difficult, but still possible (reading from
/proc on Linux, for example). Which OS do you want to use this on?

Gerhard
-- 
mail:   gerhard <at> bigfoot <dot> de       registered Linux user #64239
web:    http://highqualdev.com              public key at homepage
public key fingerprint: DEC1 1D02 5743 1159 CD20  A4B6 7B22 6575 86AB 43C0
reduce(lambda x,y:x+y,map(lambda x:chr(ord(x)^42),tuple('zS^BED\nX_FOY\x0b')))



More information about the Python-list mailing list