首页 > 解决方案 > 如何为每个进程加载 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?

标签: pythonpython-3.xpsutil

解决方案


推荐阅读