python - 如何为每个进程加载 psutil.cpu_percent 平均值?
问题描述
我正在尝试使用以下代码片段加载主机上所有进程 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?
解决方案
推荐阅读
- sql - 在 Oracle DB 中上传/存储大 CSV 文件并在后续上传时更新文件
- javascript - 关于使用 Array.push() 合并数组的问题;
- mysql - AzerothCore登录突然想要验证码
- flutter - 像 FlateButton Flutter 一样修改 ElevateButton
- flutter - 在 null 上调用 getter 'length' 导致错误屏幕
- tableau-api - Oracle ATP 连接失败并出现错误代码:7EF979EO
- flutter - Flutter如何在应用栏添加缩进和结束缩进?
- eclipse - 在 Eclipse 中模板化运行配置?
- intellij-idea - 在 IntelliJ (Windows 10) 中使用 SSH 服务器和 rsync 运行目标
- javascript - Android 11 Chrome 浏览器上网站底部的空白 Google 选项卡