问题描述
我是否可以查看当前python应用程序正在使用的处理器使用量(占最大值的百分比)?
Is it possible for me to see the amount of processor usage (% of maximum) that the current, python, app is using?
场景:只要主机不消耗超过X%的CPU能力,我的主机就可以允许我运行我的应用程序,因此我希望它关注自身"并放慢速度.那么我怎么知道该应用程序使用了多少CPU?
Scenario:My host will allow me to run my app as long as it does not consume more then X% of the CPU power, so I would like it to 'keep an eye on itself' and slowdown. So how can I know how much CPU the app is using?
目标平台是* nix,但是我也想在Win主机上这样做.
Target platform is *nix, however I would like to do it on a Win host also.
推荐答案
>>> import os
>>> os.times()
(1.296875, 0.765625, 0.0, 0.0, 0.0)
>>> print os.times.__doc__
times() -> (utime, stime, cutime, cstime, elapsed_time)
Return a tuple of floating point numbers indicating process times.
摘自(2.5)手册:
以秒为单位返回5个元组的浮点数,以指示累积的(处理器或其他)时间.这些项目是:用户时间,系统时间,儿童的用户时间,儿童的系统时间以及从过去的固定点开始按该顺序经过的实时时间.请参阅Unix手册页面times(2)或相应的Windows Platform API文档.可用性:Macintosh,Unix,Windows.
Return a 5-tuple of floating point numbers indicating accumulated (processor or other) times, in seconds. The items are: user time, system time, children's user time, children's system time, and elapsed real time since a fixed point in the past, in that order. See the Unix manual page times(2) or the corresponding Windows Platform API documentation. Availability: Macintosh, Unix, Windows.
这篇关于Python中每个进程的CPU使用率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!