python – psutil:测量特定进程的cpu使用情况

python – psutil:测量特定进程的cpu使用情况,第1张

概述我试图测量进程树的CPU使用率. 目前获取进程的cpu_usage(没有子进程)会有,但我得到了奇怪的结果. import psutilp = psutil.Process(PID)p.cpu_percent 让我回到浮动> 100,这怎么可能? 顺便说一下,PID是一个简单的pid def foo(): i = 0 while True: i += 1 据任务 我试图测量进程树的cpu使用率.

目前获取进程的cpu_usage(没有子进程)会有,但我得到了奇怪的结果.

import psutilp = 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

总结

以上是内存溢出为你收集整理的python – psutil:测量特定进程的cpu使用情况全部内容,希望文章能够帮你解决python – psutil:测量特定进程的cpu使用情况所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/langs/1193812.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-06-03
下一篇2022-06-03

发表评论

登录后才能评论

评论列表(0条)

    保存