16

The code always returns 0.0 values, regardless of interval values.

import psutil
p = psutil.Process()
print p.cpu_percent(interval=1)
print p.cpu_percent(interval=None)
Nihal Harish
  • 980
  • 5
  • 13
  • 31
  • Uhm I can reproduce it. It may be a bug in newer versions of `psutil`. (I've used other versions of the library and it always worked fine). Note that I could obtain non-zero values starting different threads in the current process. – Bakuriu Jun 23 '14 at 13:56
  • Have you tried it with `psutil.cpu_percent(interval=1)` like documented at https://code.google.com/p/psutil/wiki/Documentation#CPU? And I think when using the Process class you have to specify a PID (like `p = psutil.Process(os.getpid())`). – Christian Berendt Jun 23 '14 at 13:57
  • @ChristianBerendt: I read the documentation from the very same link and specified several PID's (valid and running), but I always got 0.0 – Nihal Harish Jun 23 '14 at 16:43
  • >>> import psutil>>> p = psutil.Process() >>> p.cpu_percent(interval=1) 0.0 >>> p.cpu_percent(interval=None) 0.0 >>> – Nihal Harish Jun 24 '14 at 07:10

4 Answers4

10

This behaviour is documented:

When interval is 0.0 or None compares process times to system CPU times elapsed since last call, returning immediately. That means the first time this is called it will return a meaningless 0.0 value which you are supposed to ignore. In this case is recommended for accuracy that this function be called a second time with at least 0.1 seconds between calls.

There is also a warning against using interval=None for a single call:

Warning: the first time this function is called with interval = 0.0 or None it will return a meaningless 0.0 value which you are supposed to ignore.

If using interval=None, make sure to call .cpu_percent compared to a prior call.

p = psutil.Process(pid=pid)
p.cpu_percent(interval=None)
for i in range(100):
    usage = p.cpu_percent(interval=None)
    # do other things

instead of:

for i in range(100):
    p = psutil.Process(pid=pid)
    p.cpu_percent(interval=None)
    # do other things
RazerM
  • 5,128
  • 2
  • 25
  • 34
whypro
  • 161
  • 2
  • 5
5

The cpu of a Process object is mutable. I have done some tests for you.

for i in range(10):
    p = psutil.Process(3301)
    print p.cpu_percent(interval=0.1)

result: 9.9 0.0 0.0 0.0 0.0 9.9 0.0 9.9 0.0 0.0

So if you want to get the CPU percent of a Process object, you could take the average in certain time.

test_list = []
for i in range(10):
    p = psutil.Process(6601)
    p_cpu = p.cpu_percent(interval=0.1)
    test_list.append(p_cpu)
print float(sum(test_list))/len(test_list)

result: 1.98

More info in picture

javaDeveloper
  • 1,403
  • 3
  • 28
  • 42
Caesar
  • 51
  • 1
  • 2
3
  • Monitor the current process, i.e., p = psutil.Process() or p = psutil.Process(os.getpid())

    1. with interval value great than 0, p.cpu_percent() will always render you 0.0, because it will block your current process. During the blocking period, the cpu percent is actually 0.0;
    2. with interval value being 0.0 or None, calling another p.cpu_percent() immediately after p.cpu_percent() will also return 0.0. The period between that two calls is too short for psutil to monitor correctly. So make sure at least 0.1 sec elapsed between two calls.
  • Monitor some other process, i.e., p = psutil.Process(other_pid)

    1. interval value great than 0 will probably get you the expected value;
    2. interval value being 0.0 or None, you still should not call the cpu_percent() function too shortly after another, reason the same as above.

Another Note:

p = psutil.Process() should be called just once and reuse p afterwards, rather than call it every time before you call p.cpu_percent(); otherwise you are actually calling cpu_percent() function the first time whenever you call p.cpu_percent(), with unexpected return value 0.0.

Ale
  • 917
  • 9
  • 28
tusonggao
  • 101
  • 5
2

From my own code that works:

cpu = psutil.cpu_times_percent(interval=0.4, percpu=False)
madclicker
  • 21
  • 3