Search code examples
pythonraspberry-picpu-load

Get CPU load in python on Raspberry Pi doesn't work


I have googled for code to get the CPU load on RPi3. I found this code:

import os

def getCPUuse():
    return str(os.popen("top -n1 | awk '/Cpu\(s\):/ {print $2}'").readline().strip())

For me it only returns an empty string.

Where is the problem in this code?

EDIT:

I call it like this:

while True:
    time.sleep(0.2)
    use = getCPUuse()
    print(use)

Solution

  • Take off the strip() and just do readline. That worked for me.