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

[RPi OS] Invalid CPU percentage values #2193

Closed
hex-developer opened this issue Jan 22, 2023 · 3 comments
Closed

[RPi OS] Invalid CPU percentage values #2193

hex-developer opened this issue Jan 22, 2023 · 3 comments
Labels

Comments

@hex-developer
Copy link

Summary

  • OS: Raspberry Pi OS Lite
  • Architecture: ARM
  • Psutil version: 5.8.0
  • Python version: Python 3.9.2
  • Type: not sure

Description

I am using psutil to return cpu usage for each core. From the python interpreter this works correctly, however from a script it does not.
Both are using the same python version.
The code I run is

import psutil
print(psutil.cpu_percent(percpu=True))

In both cases, this returns no errors.
When executing the script, I get values such as [0.0, 0.0, 0.0, 0.0] and [0.0, 0.0, 100.0, 0.0]
Running htop proves that cpu usage is different and that all cores are used.
Even rebooting failed to help.

@hex-developer
Copy link
Author

I upgraded to psutil version 5.9.4 and I still have the same results

@giampaolo
Copy link
Owner

From doc:

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

Translated: to get meaningful values you have to call psutil.cpu_percent() in a loop with an interval:

while 1:
    print(psutil.cpu_percent())
    time.sleep(1)

@hex-developer
Copy link
Author

I wrote the same code in a different way and it worked. Thank you!

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

No branches or pull requests

2 participants