Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Problems with cpu_percent() function is giving wrong information #1330

Open
manurodig opened this issue Aug 23, 2018 · 2 comments
Open

Problems with cpu_percent() function is giving wrong information #1330

manurodig opened this issue Aug 23, 2018 · 2 comments

Comments

@manurodig
Copy link

manurodig commented Aug 23, 2018

Hi I'm trying to evaluate the performance of an algorithm, that helps to analyze images. I need to get the mean and median of execution time, the CPU usage, the RAM memory used etc.

I'm using an iMac,The problem is when I tried to check the percentage of use of the CPU, I noticed that the percentage of CPU was something like this:

100
140
145
197
200
182
340
89
90
99
100
111
1000
11000

But i was checking the task manager in the iMac and always I have the same value, 120 %, 127.7 %, 111, 123, 124, 125 etc.

So I was reading that you can divide that number by the cput_count, In this case:

p = psutil.Process(os.getpid())
cpu=p.cpu_percent() / psutil.cpu_count()

I have 4 cpus, so at the end even if I divide the cpu_percent with the cpu_count, I'm going to get results less than the task manager.

So, I think the function is not calculating in a good way the cpu percentage.

Could you help me or suggest something?

@giampaolo
Copy link
Owner

Hello. What sleep/interval are you using between calls? For instance, this:

for x in range(100):
     print(p.cpu_percent())

...will give you wrong results because the time between calls is too short. Instead you should do:

for x in range(100):
     print(p.cpu_percent(interval=1))

...or if you don't want to block execution make sure to call p.cpu_percent with some delay between calls.

@kaimast
Copy link

kaimast commented Dec 5, 2018

How is it possible that the percentage returned is larger than the number of logical cores? I run print(cpu_percent(interval=1.0) / cpu_count()) in a loop on Linux and sometimes get values of >100.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants