python 怎么获得cpu使用率
发布网友
发布时间:2022-04-24 21:07
我来回答
共2个回答
热心网友
时间:2022-04-19 00:05
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()
热心网友
时间:2022-04-19 01:23
什么操作系统?