[英]CPU Usage Per Process in Python
Is it possible for me to see the amount of processor usage (% of maximum) that the current, python, app is using? 我是否可以看到当前python应用程序使用的处理器使用量(最大值的百分比)?
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. 场景:我的主机允许我运行我的应用程序,只要它不消耗超过X%的CPU功率,所以我希望它“关注自己”并放慢速度。 So how can I know how much CPU the app is using? 那么我怎么知道应用程序使用了多少CPU?
Target platform is *nix, however I would like to do it on a Win host also. 目标平台是* nix,但是我想在Win主机上也这样做。
>>> 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.
From the (2.5) manual: 从(2.5)手册:
times( ) 次()
Return a 5-tuple of floating point numbers indicating accumulated (processor or other) times, in seconds. 返回一个5元组的浮点数,表示累计(处理器或其他)时间,以秒为单位。 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. 请参阅Unix手册页时间(2)或相应的Windows Platform API文档。 Availability: Macintosh, Unix, Windows. 可用性:Macintosh,Unix,Windows。
The resource
module provides getrusage
which can give you the information you need, at least for Unix-like platforms. resource
模块提供getrusage
,可以为您提供所需的信息,至少对于类Unix平台而言。
Note that CPU usage as a percentage is always measured over a time interval. 请注意,CPU使用率百分比始终是在一段时间内测量的。 Essentially, it is the amount of time taken by your program doing something divided by the interval time. 从本质上讲,它是程序执行某些操作所花费的时间除以间隔时间。
For example, if your application takes 2 seconds of CPU time over a 5 second period, then it can be said to be using 40% of the CPU. 例如,如果您的应用程序在5秒的时间内占用2秒的CPU时间,那么可以说它占用了40%的CPU。
Note that this calculation, as simple as it seems, can get tricky when using a multiprocessor system. 请注意,使用多处理器系统时,这种计算很简单,可能会变得棘手。 If your application uses 7 seconds of CPU time in 5 seconds of wall clock time on a two-processor system, do you say it is uses 140% or 70% CPU? 如果您的应用程序在双处理器系统上在5秒的挂钟时间内使用7秒的CPU时间,您说它是使用140%还是70%的CPU?
Update: As gimel mentions, the os.times
function also provides this information in a platform-independent way. 更新:正如gimel所提到的, os.times
函数还以独立于平台的方式提供此信息。 The above calculation notes still apply, of course. 当然,上述计算说明仍然适用。
Use time.clock()
to get the CPU time. 使用time.clock()
来获取CPU时间。 To get the percentage of CPU usage do CPU time elapsed/time elapsed 要获得CPU使用百分比,请执行CPU时间/经过的时间
For example, if CPU time elapsed is 0.2 and time elapsed is 1 then the cpu usage is 20%. 例如,如果经过的CPU时间为0.2且经过的时间为1,则CPU使用率为20%。
Note:You have to divide by by number of processers you have. 注意:您必须除以您拥有的处理器数量。 If you have 2 ie a dual core: 如果你有2即双核:
import decimal,timeit
decimal.getcontext().prec=1000
def torture():
decimal.Decimal(2).sqrt()
time.sleep(0.1)
import time
clock=time.clock()
while 1:
clock=timeit.timeit(torture,number=10)
tclock=time.clock()
print((tclock-clock)*p)
clock=tclock
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.