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 ...
随机推荐
- RESTful API 设计最佳实践(转)
摘要:目前互联网上充斥着大量的关于RESTful API(为了方便,以后API和RESTful API 一个意思)如何设计的文章,然而却没有一个”万能“的设计标准:如何鉴权?API格式如何?你的API ...
- HDU 4602 Magic Ball Game(离线处理,树状数组,dfs)
Magic Ball Game Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) ...
- Linux-Ubuntu 启用root账户
Ubuntu Linux有一个与众不同的特点,那就是初次使用时,你无法作为root来登录系统,为什么会这样?这就要从系统的安装说起.对于其他Linux系统来 说,一般在安装过程就设定root密码,这样 ...
- Umbraco中更换IndexSet中的NodeType后,搜索页面没有做出对应更改的效果
在项目开发中,使用ExternalSearcher,有一个ExamineIndex.config文件中存放ExternalIndexSet 开始时是这样的 <!-- Default Indexs ...
- POJ-2785 4 Values whose Sum is 0(折半枚举 sort + 二分)
题目链接:http://poj.org/problem?id=2785 题意是给你4个数列.要从每个数列中各取一个数,使得四个数的sum为0,求出这样的组合的情况个数. 其中一个数列有多个相同的数字时 ...
- springMVC+JAP整合彻底摆脱persistence.xml配置文件
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.spr ...
- 数据库:mongodb与关系型数据库相比的优缺点
与关系型数据库相比,MongoDB的优点:①弱一致性(最终一致),更能保证用户的访问速度:举例来说,在传统的关系型数据库中,一个COUNT类型的操作会锁定数据集,这样可以保证得到“当前”情况下的精 ...
- installshield Basic 工程每次安装完提示重启电脑
将Sequence中的ScheduleReboot Action的Condition清空即可.
- 如何设置box shadow的透明度
(从已经死了一次又一次终于挂掉的百度空间人工抢救出来的,发表日期2014-04-24) 今天发现使用box-shadow属性,可以很好的给div添加阴影效果,但是添加的效果如果是: -moz-box- ...
- PL/pgSQL函数带output参数例子
例子1,不带returns : [postgres@cnrd56 bin]$ ./psql psql () Type "help" for help. postgres=# CRE ...