1

python -c "import psutil;print(psutil.cpu_percent())"

Output: 0.0

python # we're in the interpreter now

import psutil

print(psutil.cpu_percent())

output: 12.7

what's going on here? when running it outside of the interpreter I always get either 0.0 or 100.0 so I assume it's reporting only itself's cpu usage percentage in this case.

But I'm curious as to why and how the same code isn't acting consitently as a script vs being executed in the interpreter.

Found the 'expected behavior' of the lib here: CPU usage of python script

Community
  • 1
  • 1
JackofSpades
  • 113
  • 1
  • 8
  • try to run it an a loop to get more than just one value – hek2mgl May 24 '19 at 11:03
  • Give the process a little time to allow measurable runtime: `python -c "import psutil;print(psutil.cpu_percent(0.1))"` and read the red warning at https://psutil.readthedocs.io/en/latest/#psutil.Process.cpu_percent . – Klaus D. May 24 '19 at 11:05
  • Adding an interval is exactly what 'fixes' this. Although I am rather curious as to why this happens. – JackofSpades May 26 '19 at 06:08

0 Answers0