python 怎么获得cpu使用率
2个回答
推荐于2016-04-11 · 知道合伙人互联网行家
关注
展开全部
python 获取代码 :
#!/usr/bin/python
# -*- coding:utf8 -*-
__author__ = 'chenwx'
def cpu_rate():
import time
def cpu_r():
f = open("/proc/stat","r")
for f_line in f:
break
f.close()
f_line = f_line.split(" ")
f_line_a=[]
for i in f_line:
if i.isdigit():
i=int(i)
f_line_a.append(i)
total = sum(f_line_a)
idle = f_line_a[3]
return total,idle
total_a,idle_a=cpu_r()
time.sleep(2)
total_b,idle_b=cpu_r()
sys_idle = idle_b - idle_a
sys_total = total_b - total_a
sys_us = sys_total - sys_idle
cpu_a = (float(sys_us)/sys_total)*100
return cpu_a
# print cpu_rate()
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询