我已经搜索了代码以在RPI3上获得CPU负载。我找到了此代码:
import os
def getCPUuse():
return str(os.popen("top -n1 | awk '/Cpu(s):/ {print $2}'").readline().strip())
对我来说,它只会返回一个空字符串。
此代码中的问题在哪里?
编辑:
我这样称呼:
while True:
time.sleep(0.2)
use = getCPUuse()
print(use)
取下strip()
,然后读取线。那对我有用。
您可以使用 gpiozero
模块 - 它与覆盆子Pi OS预装。因此,您可以使用此代码,也更容易阅读(我认为):
from gpiozero import LoadAverage
print(str(int(LoadAverage(minutes=1).load_average*100))+"%")
编辑:此链接可能会有所帮助:https://gpiozero.readthedocs.io/en/stable/api_internal.html#loadaverage