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 ...
随机推荐
- webrtc--AudioProcessing的使用
1.AudioProcessing的实例化和配置: AudioProcessing* apm = AudioProcessing::Create(0); apm->level_estimator ...
- iPhone 微信平台链接到微信文章 返回上一页问题
东钿金融服务平台 有个隐藏按钮,点击此按钮,会弹出一个九宫格的功能栏,其中有个‘工作时间‘,这项,它是链接到微信公众号里面的一篇文章,按照平常写法就是 直接把文章链接地址 赋在a的href上,但是iP ...
- Castle学习笔记----初探IOC容器
通常IOC实现的步骤为-->建立容器-->加入组件-->获取组件-->使用组件. 1.建立容器 建立容器也就是IWindsorContainer.接着我门要向容器中注册服务,并 ...
- Session的实现与存储
[集群中session安全和同步] 使用数据库来保存session,就算服务器宕机了也没事,session照样在. 每次请求都进行数据库读写开销不小(使用内存数据库可以提高性能,宕机就会丢失数据. ...
- cmd命令。
CMD按任意退出 echo 退出……按任意键pause & exit
- js 原型的内存分析
使用构造器的弊端:http://www.cnblogs.com/a757956132/p/5258897.html 示例 将行为设置为全局的行为,如果将所有的方法都设计为全局函数的时候, 这个函数就可 ...
- NameThreadForDebugging -- Naming threads for debugging
http://forums.devart.com/viewtopic.php?t=16907 type tagTHREADNAME_INFO = record dwType : LongWord; / ...
- PL/pgSQL RETURNS TABLE 例子
实验如下: RETURNS TABLE 中的变量名和SQL文中的变量名同名时,执行时会出错: pgsql=# create table sales(itemno integer,quantity in ...
- PostgreSQL停止动作观察
实验过程如下: 启动一个客户端: [postgres@cnrd56 bin]$ ./psql psql () Type "help" for help. postgres=# be ...
- Orchard官方文档
开始使用 安装Orchard 通过Orchard zip文件安装 使用WebMatrix开发Orchard Dashboard总览 创建你的第一个Orchard站点 导航和菜单 添加博客 新增管理媒体 ...