Closed GoogleCodeExporter closed 8 years ago
In order to obtain a meaningful result you should call get_cpu_percent()
different times, in a loop, with 1 second interval.
That way you should observe results which are *similar* to taskmgr's.
What it seems you're doing is that you called get_cpu_percent() once (or twice,
with NO timeout between calls), then complained because taskmgr.exe shows
something different.
Original comment by g.rodola
on 20 Feb 2014 at 10:25
As you can see the code snippet which I posted, there is a function between the
two get_cpu_percent() call. That particular function will sleep for around 10 -
20 seconds approximately.
I need to get the cpu usage used by that particular function call?Is there a
way to get that data?
Original comment by bharathi...@gmail.com
on 20 Feb 2014 at 11:04
You can get the CPU percentage of a given *process*, not a given function.
You can't do what you're trying to do because it doesn't make sense.
What you can do, if you want to time a function, is to benchmark it, as in:
>>> t = time.time()
>>> yourfun()
>>> print(time.time() - t)
0.08523121315878872
...or (better) by using timeit module:
>>> import timeit
>>> min(timeit.repeat(yourfun))
0.07578110694885254
...then apply improvements to "yourfun" in order to decrease execution time,
benchmark again, etc. until you're satisfied (or give up).
You can also use p.get_cpu_times() in order to differentiate between user and
system times, but that's something "extra" and you're probably not interested
in that.
I think what you're looking for is what I did in the first code sample.
Original comment by g.rodola
on 20 Feb 2014 at 11:25
Thanks for the data, but our client has requested to give them the Cpu Usage
for that call.
As per my understanding of get_cpu_percent() call is:
When we first call the process.get_cpu_percent(interval=1), it just remembers
the value of user and system times and when we once again call the
process.get_cpu_percent(interval=0),it gives the user and system times
subtracted with the old value stored something like time= endtime - starttime
functionality.
If thats the functionality of get_cpu_percent() why shouldn't it returns the
accurate value?
Original comment by bharathi...@gmail.com
on 20 Feb 2014 at 11:33
And Its fine if I get the CpuUsage for the process pythonw.exe at the
particular point of function call.
Original comment by bharathi...@gmail.com
on 20 Feb 2014 at 11:37
You should use interval=0 on the first call as in:
p = psutil.Process(os.getpid())
p.get_cpu_percent(timeout=0)
yourcall()
print(p.get_cpu_percent(timeout=0))
IMO this is not very useful but there you have it.
You cannot really compare the value you get out of that with taskmgr's because
the timings are just different and you basically end up comparing orange and
apples.
Original comment by g.rodola
on 20 Feb 2014 at 11:41
Yes I know I shouldn't compare orange and apples. But In my case, I will be
running my application by displaying the output and task manager at the same
time. So that it may vary around 10% but not around 30-40% difference right?.
Anyway as you suggested I try to work on it and will get back to you tomorrow
with the result.
Original comment by bharathi...@gmail.com
on 20 Feb 2014 at 11:49
> But In my case, I will be running my application by
> displaying the output and task manager at the same time.
Yes, but the taskmgr will measure CPU percent with an interval of 1 sec on a
total time span of, say, 20 secs (the total time of your call) whereas psutil
will use an interval of 20 secs on a total time span of 20 secs and provide a
single value instead of 20.
That's why I say you can't compare the two things.
In order to have a "real" comparison psutil should do the same as taskmgr.
You can emulate that by using a thread, like this:
import threading, psutil
def print_cpu_percent():
while 1:
print p.cpu_percent(interval=1)
p = psutil.Process(os.getpid())
t = threading.Thread(target=print_cpu_percent)
t.start()
yourfun()
Original comment by g.rodola
on 20 Feb 2014 at 12:15
OK finally how ll I get the CPU usage here after calling my function...
Original comment by bharathi...@gmail.com
on 20 Feb 2014 at 1:15
Hi,
In the above code, the line " print p.cpu_percent(interval=1)" we can't call
the cpu_percent() with the particular process ID since its not a member of the
Process class. It also gives us the system wide CPU utilization and not the
process value.
I replaced the same code instead of cpu_percent with get_cpu_percent() but I am
still not getting the expected result. It giving a value of 100% for most of
the cases when the task manager is showing around 40% of cpu Usage.
The cpu_percent() function is giving the similar value to taskmanager and why
not the get_cpu_percent() is not displaying the expected result.
Please someone help me to get the cpu usage of a particular process.
Original comment by bharathi...@gmail.com
on 28 Feb 2014 at 4:47
I cannot reproduce the issue you describe and you fail to provide any useful
info in order to fix it or reproduce it.
Keep shouting "please somebody help me" is not gonna help you get your problem
fixed.
Closing this out.
Original comment by g.rodola
on 28 Feb 2014 at 11:56
bharathi...@gmail.com I think you can use performance counters in Windows to
get the task manager percentage.
g.rodola@, I tried your approach but the values I get are still around 100% for
a process that shows 25% on task manager consistently.
Maybe Task manager is using some kind of averaging over the a sliding window ?
Original comment by nomad...@google.com
on 9 Apr 2015 at 8:04
Original issue reported on code.google.com by
bharathi...@gmail.com
on 20 Feb 2014 at 9:45