我试图测量进程树的CPU使用率.
目前获取进程的cpu_usage(没有子进程)会有,但我得到了奇怪的结果.
import psutil
p = psutil.Process(PID)
p.cpu_percent
让我回到浮动> 100,这怎么可能?
顺便说一下,PID是一个简单的pid
def foo():
i = 0
while True:
i += 1
据任务经理说,它的CPU使用率约为12%
我想获得12.5或类似的输出.
解决方法
我读了一些关于psutil的文档,这是我得到的:
Note: a percentage > 100 is legitimate as it can result from a process with >multiple threads running on different CPU cores.
所以为了摆脱> 100,你应该做这样的事情:
Note: the returned value is explcitly not split evenly between all CPUs cores (differently from psutil.cpu_percent()). This means that a busy loop process running on a system with 2 CPU cores will be reported as having 100% CPU utilization instead of 50%. This was done in order to be consistent with UNIX’s “top” utility and also to make it easier to identify processes hogging CPU resources (independently from the number of CPU cores). It must be noted that in the example above taskmgr.exe on Windows will report 50% usage instead. To emulate Windows’s taskmgr.exe behavior you can do:
p.cpu_percent() / psutil.cpu_count().
由于我从其他地方得到了这个答案,我会给你一个检查它的链接 出:http://psutil.readthedocs.io/en/release-2.2.1/#psutil.Process.cpu_percent
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|