我正在尝试使用以下代码片段在主机上加载所有进程ID的CPU平均数:
import psutil
while True:
for pid in psutil.pids():
p = psutil.Process(pid)
cpu_p = float(p.cpu_percent())
if cpu_p == float(100.0)
break
elif cpu_p > float(0.0):
print(cpu_p)
问题在于结果继续返回0.0结果。在psutil文档中,它指出(参考:https://psutil.readthedocs.io/en/latest/#functions):
Warning: the first time this function is called with interval = 0.0
or None it will return a meaningless 0.0 value which you are supposed
to ignore.
如何为循环中的cpu_percentage创建合适的返回值,而不是不断返回0.0?