Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Raspberry-Linux-Python-getCPU与os.popen一起使用_Python_Linux_Background_Raspberry Pi_Cpu - Fatal编程技术网

Raspberry-Linux-Python-getCPU与os.popen一起使用

Raspberry-Linux-Python-getCPU与os.popen一起使用,python,linux,background,raspberry-pi,cpu,Python,Linux,Background,Raspberry Pi,Cpu,在我的Raspberry pi 3中,我有一个Python脚本,它在后台启动时打开。在脚本中,有一个函数获取CPU使用百分比 功能如下: `def getCPUuse(): return(str(os.popen("top -n1 | awk '/Cpu\(s\):/ {print $2}'").readline().strip()))` 如果我手动运行该程序,函数会正确地向我返回值,相反,如果程序在后台启动时加载,函数不会向我返回任何值 我到处找,但我不明白为什么 有人能帮我吗 谢谢

在我的Raspberry pi 3中,我有一个Python脚本,它在后台启动时打开。在脚本中,有一个函数获取CPU使用百分比

功能如下:

`def getCPUuse():
    return(str(os.popen("top -n1 | awk '/Cpu\(s\):/ {print $2}'").readline().strip()))`
如果我手动运行该程序,函数会正确地向我返回值,相反,如果程序在后台启动时加载,函数不会向我返回任何值

我到处找,但我不明白为什么

有人能帮我吗

谢谢


PS我不想使用psutil库

我一个人设法解决了这个问题

我发布解决方案,以便它可以帮助他人

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

可能会考虑使用完整的路径来选择<代码>顶部>代码>和<代码> AWK<代码>。如果不知道在后台开始加载程序时是如何加载的,我们无法帮助您。。。