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 ...
随机推荐
- SurfaceView 和 TextureView
1.区别 The followings are two limitations of SurfaceView: You can not be animated, transformed and sca ...
- spring properties resolve 问题
在stackoverflow上看到一个问题 配置如下: <context:property-placeholder location="/WEB-INF/application-cus ...
- AutoCAD DxfCode组码值类型
0-9 字符串(随着从 AutoCAD 2000 起引入了扩展符号名称,字数限制已由 255 个字符扩大到 2049 个单字节字符,不包括行末的换行符) 10-39 双精度三维点值 40-59 双精度 ...
- DELPHI7在WIN8和WIN10下安装和运行
DELPHI7在WIN8下安装后可以打开运行,但发现设置断点DEBUG运行DLL工程时会卡死(IDE长时间无反应,不报错). DELPHI7在WIN10下安装后打开的时候会报错,无法运行. 以上两种情 ...
- 获取某几个分类下的前N条数据 mssql语句
方案1: (SELECT top 10 * FROM 表 where type=3 ) UNION ALL (SELECT top 10 * FROM 表 where type=4 ) ...
- C#和JavaScript的区别
Strong and Loose Typing: 强弱比较 // C# var customer = new Customer(); //var is compiler inferred //Java ...
- CSS Gradient文字效果
你想创建的标题没有渲染和Photoshop每个标题吗?这里是一个简单的CSS技巧向您展示如何创建渐变文字效果,PNG图像(纯CSS,没有Javascript或Flash).你所需要的是一个空的< ...
- Study notes for Clustering and K-means
1. Clustering Analysis Clustering is the process of grouping a set of (unlabeled) data objects into ...
- 标准C++中的STL容器类简单介绍
SGI -- Silicon Graphics[Computer System] Inc.硅图[计算机系统]公司. STL -- Standard Template Library 标准模板库. ...
- oracle 11g 没有scott用户下emp的创建方法
oracle 11g 安装后 没有scott 用户, 创建scott 用户后 使用select * from emp查询 emp表, 结果为 找不到行. 运行脚本 utlsample.sql 首先as ...