如何在不使用PSUti的情况下获得python2.7的CPU使用率

2024-10-06 11:20:29 发布

您现在位置:Python中文网/ 问答频道 /正文

正在尝试在Python中获取CPU使用率而不使用PSUtil。在

我试过下面的方法,但结果总是一样的。。。在

def getCPUuse():
    return(str(os.popen("top -n1 | awk '/Cpu\(s\):/ {print $2}'").readline().strip(\
)))

print(getCPUuse())

这似乎总是报告3.7%,即使当我加载CPU。在

我也试过以下几点。。。在

^{pr2}$

这似乎总是返回5.12。我得承认我不知道上面说的是什么。如果我在命令行中输入grep cpu /proc/stat,我会得到这样的结果。。。在

cpu  74429 1 19596 1704779 5567 0 284 0 0 0
cpu0 19596 0 4965 422508 1640 0 279 0 0 0
cpu1 18564 1 4793 427115 1420 0 1 0 0 0
cpu2 19020 0 4861 426916 1206 0 2 0 0 0
cpu3 17249 0 4977 428240 1301 0 2 0 0 0

我猜我的命令没有正确地从上面的输出中提取所有CPU核心的值?在

我的目标是在不使用PSUtil的情况下从我的设备(Raspberry PI)中获取总CPU%。该图应反映操作系统任务管理器中显示的内容。在


Tags: 方法returnostopdefcpupsutilprint
2条回答

这并不容易,因为您描述的大多数进程都提供了CPU使用率的累积或总平均值。在

也许您可以尝试使用tesystat包附带的mpstat命令。在

因此,我在以下脚本中使用的步骤是:

  1. 要求mpstat生成两个报告,一个现在,另一个在1秒后(mpstat 1 2
  2. 然后我们得到Average行(最后一行)
  3. 最后一列是%idle列,因此我们用awk中的$NF变量得到它
  4. 我们使用来自子进程的Popen,但是设置shell=True来接受管道(|)选项。在
  5. 我们执行命令(communicate()
  6. 使用strip清除输出
  7. 从100减去空闲百分比,我们就可以得到使用的值。在

因为它将休眠1 second,所以不要担心它不是一个即时命令。在

import subprocess                                      

cmd = "mpstat 1 2 | grep Average | awk '{print $NF}'"
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, shell=True)                                   
out, err = p.communicate()                   
idle = float(out.strip())                              
print(100-idle) 

PSUtil、htop、mpstat等都是从/proc/stat读取以"cpu"开头的行(实际上是第一行),然后根据该行中的值计算一个百分比。您可以在man 5 proc(搜索“proc/stat”)中找到该行中值的含义。在

这也是您提到的grep cpu /proc/stat | awk ....命令的作用。但是/proc/stat中的值代表自上次启动以来花费的时间!也许过了一段时间,我不确定,但关键是这些数据是经过很长时间测量的。在

因此,如果您运行该命令,并在几秒钟(分钟甚至数小时)后再次运行它,它们不会有太大的变化!这就是为什么你看到它总是返回5.12。在

top这样的程序会记住以前的值,并从新读取的值中减去它们。根据这个结果,可以计算出一个实际反映最近CPU负载的“实时”百分比。在

为了尽可能简单地在python中执行类似的操作,但不运行外部命令来读取/proc/stat并为我们进行计算,我们可以将读取的值存储到一个文件中。下一次运行时,我们可以读回它们,并从新值中减去它们。在

#!/usr/bin/env python2.7

import os.path

# Read first line from /proc/stat. It should start with "cpu"
# and contains times spent in various modes by all CPU's totalled.
#
with open("/proc/stat") as procfile:
    cpustats = procfile.readline().split()

# Sanity check
#
if cpustats[0] != 'cpu':
    raise ValueError("First line of /proc/stat not recognised")

#
# Refer to "man 5 proc" (search for /proc/stat) for information
# about which field means what.
#
# Here we do calculation as simple as possible:
# CPU% = 100 * time_doing_things / (time_doing_things + time_doing_nothing)
#

user_time = int(cpustats[1])    # time spent in user space
nice_time = int(cpustats[2])    # 'nice' time spent in user space
system_time = int(cpustats[3])  # time spent in kernel space

idle_time = int(cpustats[4])    # time spent idly
iowait_time = int(cpustats[5])    # time spent waiting is also doing nothing

time_doing_things = user_time + nice_time + system_time
time_doing_nothing = idle_time + iowait_time

# The times read from /proc/stat are total times, i.e. *all* times spent
# doing things and doing nothing since last boot.
#
# So to calculate  meaningful CPU % we need to know how much these values
# have *changed*.  So we store them in a file which we read next time the
# script is run.
# 
previous_values_file = "/tmp/prev.cpu"
prev_time_doing_things = 0
prev_time_doing_nothing = 0

try:
    with open(previous_values_file) as prev_file:
        prev1, prev2 = prev_file.readline().split()
    prev_time_doing_things = int(prev1)
    prev_time_doing_nothing = int(prev2)
except IOError:  # To prevent error/exception if file does not exist. We don't care.
    pass   

# Write the new values to the file to use next run
#
with open(previous_values_file, 'w') as prev_file:
    prev_file.write("{} {}\n".format(time_doing_things, time_doing_nothing))

# Calculate difference, i.e: how much the number have changed
#
diff_time_doing_things = time_doing_things - prev_time_doing_things
diff_time_doing_nothing = time_doing_nothing - prev_time_doing_nothing

# Calculate a percentage of change since last run:
#
cpu_percentage = 100.0 * diff_time_doing_things/ (diff_time_doing_things + diff_time_doing_nothing)

# Finally, output the result
#
print "CPU", cpu_percentage, "%"

这是一个与top不同的版本,它每秒打印CPU使用率,在变量中而不是在文件中记住先前测量的CPU时间:

^{pr2}$

相关问题 更多 >