kiranvizru / psutil

Automatically exported from code.google.com/p/psutil
Other
0 stars 0 forks source link

Make cpu_percent() timeout default to 0.0 #463

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Currently we have 3 functions for calculating CPU percentage:

psutil.Process.get_cpu_percent()
psutil.cpu_percent()
psutil.cpu_times_percent()

All of them have a "timeout" parameter which defaults to 0.1 seconds.
We originally did that because waiting some time was necessary in order to get 
a meaningful return value. 

I've been regretting that decision many times though, because in applications 
which call cpu_percent* repeatedly in a loop it's very easy to forget about 
specifying timeout=0 resulting in dramatic slowdowns which are not immediately 
easy to debug.
Typical example is this, which will take *seconds* to complete:

for p in psutil.process_iter():
    pinfo = {}
    try:
        pinfo['cpu_percent'] = p.cpu_percent()
        # collect other systtem info...
    except psutil.NoSuchProcess:
        pass

Proposal is to just have timeout=0.0 by default and make it clear in the doc 
that if no timeout is specified the first call to *cpu_percent() will return a 
meaningless value (0.0).

Original issue reported on code.google.com by g.rodola on 7 Jan 2014 at 8:00

GoogleCodeExporter commented 9 years ago

Original comment by g.rodola on 7 Jan 2014 at 8:59

GoogleCodeExporter commented 9 years ago
Fixed in revision 229c7155fd00.

Original comment by g.rodola on 9 Mar 2014 at 10:28

GoogleCodeExporter commented 9 years ago
Closing out as fixed as 2.0.0 version is finally out.

Original comment by g.rodola on 10 Mar 2014 at 11:36