python获取每颗cpu使用率
以下是关于python来获取服务器每颗CPU使用率的使用脚本。
#!/usr/bin/env python
# -*- coding: utf-8 -*- import re,time def _read_cpu_usage():
"""Read the current system cpu usage from /proc/stat"""
statfile = "/proc/stat"
cpulist = []
try:
f = open(statfile, 'r')
lines = f.readlines()
except:
print "ERROR: Can not open %s,The system cannot continue to run" % (statfile)
return []
for line in lines:
tmplist = line.split()
if len(tmplist) < 5:
continue
for b in tmplist:
m = re.search(r'cpu\d+',b)
if m is not None:
cpulist.append(tmplist)
f.close()
return cpulist def get_cpu_usage():
cpuusage = {}
cpustart = {}
cpuend = {}
linestart = _read_cpu_usage()
if not linestart:
return 0
for cpustr in linestart:
usni=long(cpustr[1])+long(cpustr[2])+long(cpustr[3])+long(cpustr[5])+long(cpustr[6])+long(cpustr[7])+long(cpustr[4])
usn=long(cpustr[1])+long(cpustr[2])+long(cpustr[3])
cpustart[cpustr[0]] = str(usni)+":"+str(usn)
sleep = 2
time.sleep(sleep)
lineend = _read_cpu_usage()
if not lineend:
return 0
for cpustr in lineend:
usni=long(cpustr[1])+long(cpustr[2])+long(cpustr[3])+long(cpustr[5])+long(cpustr[6])+long(cpustr[7])+long(cpustr[4])
usn=long(cpustr[1])+long(cpustr[2])+long(cpustr[3])
cpuend[cpustr[0]] = str(usni)+":"+str(usn)
for line in cpustart:
start = cpustart[line].split(':')
usni1,usn1 = float(start[0]),float(start[1])
end = cpuend[line].split(':')
usni2,usn2 = float(end[0]),float(end[1])
cpuper=(usn2-usn1)/(usni2-usni1)
cpuusage[line] = int(100*cpuper) return cpuusage if __name__ == '__main__':
a = get_cpu_usage()
print a
python获取每颗cpu使用率的更多相关文章
- Java获取Linux系统cpu使用率
原文:http://www.open-open.com/code/view/1426152165201 import java.io.BufferedReader; import java.io.Fi ...
- 获取Windows操作系统的CPU使用率以及内存使用率
此功能参考了ProcessHacker项目的代码. 声明定义 typedef struct _UINT64_DELTA { ULONG64 Value; ULONG64 Delta; } UINT64 ...
- [Python Study Notes]计算cpu使用率v0.1
V0.1 更新日志: 1.加入平台判断,支持windows与linux 2.自动清屏显示,显示更加直观 '''''''''''''''''''''''''''''''''''''''''''''''' ...
- [Python Study Notes]计算cpu使用率
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...
- [No0000112]ComputerInfo,C#获取计算机信息(cpu使用率,内存占用率,硬盘,网络信息)
github地址:https://github.com/charygao/SmsComputerMonitor 软件用于实时监控当前系统资源等情况,并调用接口,当资源被超额占用时,发送警报到个人手机: ...
- Golang利用第三方包获取本机cpu使用率以及内存使用情况
第三方包下载 $ github.com/shirou/gopsutil 获取内存方面的信息 package main import ( "fmt" "github.com ...
- 为什么在Python里推荐使用多进程而不是多线程?(为什么python多线程无法增加CPU使用率?)
最近在看Python的多线程,经常我们会听到老手说:“Python下多线程是鸡肋,推荐使用多进程!”,但是为什么这么说呢? 要知其然,更要知其所以然.所以有了下面的深入研究: 首先强调背景: ...
- Python 获取服务器的CPU个数
在使用gunicorn时,需要设置workers, 例如: gunicorn --workers=3 app:app -b 0.0.0.0:9000 其中,worker的数量并不是越多越好,推荐值是C ...
- 使用shell脚本获取虚拟机中cpu使用率(读/proc/statc)
#!/bin/bash interval= cpu_num=`-] -c` start_idle=() start_total=() cpu_rate=() cpu_rate_file=./`host ...
随机推荐
- 记拿到鹅厂前端开发暑期实习offer的经历
#想起来时的路 在真正拿到腾讯实习offer之前,也是看过不少人的面经,心生向往.很早在入前端坑之前,我就想着大四的时候有机会要尝试去腾讯里实习. 大一入门语言就是C++,这让我很无奈,所以我很快的就 ...
- [原创]Devexpress XtraReports 系列 3 创建主从报表
昨天写了系列的第二篇Devexpress XtraReports 系列 2 创建表格报表 . 今天我们来继续系列 3 创建主从报表 首先我们来看看最后实现的效果.Demo最后附上. 开始吧. 第一步, ...
- POJ 1556 The Doors(线段交+最短路)
The Doors Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5210 Accepted: 2124 Descrip ...
- 删除qq历史签名
我们在设置新的个性签名的时候之前的签名会被记录,我们可以用手机qq删除这些历史签名,告别过去,做崭新的自己. 到需要删除的历史签名, 从右至左滑动屏幕
- 【LeetCode】 Subsets
Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be ...
- 安装、设置与启动MySql5.1.30绿色版的方法
1.解压 mysql-noinstall-5.1.30-win32.zip(下载地址http://dev.mysql.com/downloads/mysql/5.1.html) 2.在 F 盘建立目录 ...
- Maven配置文件说明
<projectxmlns="http://maven.apache.org/POM/4.0.0 " xmlns:xsi="http://www.w3. ...
- 10个经典的Java main 方法面试题
1. 不用main方法如何定义一个类? 不行,没有main方法不能运行Java类. 在Java 7之前,你可以通过使用静态初始化运行Java类.但是,从Java 7 开始就不行了. 2. main() ...
- 创建类模式(五):单例(Singleton)
定义 确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例. 单例模式一般情况下通过使用private的构造函数确保了在一个应用中只产生一个实例,并且是自行实例化. 和静态变量的区别 虽然 ...
- easyui中tree型控件不正常显示的处理方法
我在使用easyui中的tree控件时,出现不正常显示的现象,比如li中不能使用自定义的图标.父级展开或关闭时,其子级仍然显现并出现重叠等.找了很多资料,都没解决这个问题,后来逐个对照官方的源码,才找 ...