• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > Python获取单个程序CPU使用情况趋势图

Python获取单个程序CPU使用情况趋势图

作者:junjie 字体:[增加 减小] 来源:互联网

junjie 通过本文主要向大家介绍了python 获取cpu信息,python 获取cpu,python趋势,python发展趋势,python示例程序等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文定位:已将CPU历史数据存盘,等待可视化进行分析,可暂时没有思路。
前面一篇文章(http://www.jb51.net/article/61956.htm)提到过在linux下如何用python将top命令的结果进行存盘,本文是它的后续。

python中我们可以用matplotlib很方便的将数据可视化,比如下面的代码:
import matplotlib.pyplot as plt

list1 = [1,2,3]
list2 = [4,5,9]
plt.plot(list1,list2)
plt.show()
</div>

执行效果如下:

上面只是给plot函数传了两个list数据结构,show一下图形就出来了……哈哈,很方便吧!
获取CPU趋势图就用这个了!
可我们现在得到的数据没那么友好,比如我现在有个文件(file.txt),内容如下:

Cpu(s): 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu(s): 7.7%us, 7.7%sy, 0.0%ni, 76.9%id, 0.0%wa, 0.0%hi, 7.7%si, 0.0%st
Cpu(s): 0.0%us, 9.1%sy, 0.0%ni, 90.9%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu(s): 9.1%us, 0.0%sy, 0.0%ni, 90.9%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu(s): 8.3%us, 8.3%sy, 0.0%ni, 83.3%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu(s): 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu(s): 0.0%us, 9.1%sy, 0.0%ni, 90.9%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
Cpu(s): 0.0%us, 0.0%sy, 0.0%ni,100.0%id, 0.0%wa, 0.0%hi, 0.0%si, 0.0%st
</div>

其中,第一列为时间,第六列为CPU的idle值。

要从这组数据中得出CPU使用情况趋势图,我们就要做些工作了。

下面是代码,这里提供一个思路,需要的朋友拷回去改一下吧:
#coding:utf-8
'''
      File      : cpuUsage.py
      Author    : Mike
      E-Mail    : Mike_Zhang@live.com
'''
import matplotlib.pyplot as plt
import string

def getCpuInfData(fileName):
    ret = {}
    f = open(fileName,"r")
    lineList = f.readlines()
    for line in lineList:
        tmp = line.split()
        sz = len(tmp)
        t_key = string.atoi(tmp[0]) # 得到key
        t_value = 100.001-string.atof(line.split(':')[1].split(',')[3].split('%')[0]) # 得到value
        print t_key,t_value   
        if not ret.has_key(t_key) :
            ret[t_key] = []
        ret[t_key].append(t_value)
    f.close()
    return ret
   
retMap1 = getCpuInfData("file.txt")
# 生成CPU使用情况趋势图
list1 = retMap1.keys()
list1.sort()
list2 = []
for i in list1:list2.append(retMap1[i])
plt.plot(list1,list2)
plt.show()
</div>

好,就这些了,希望对你有帮助。

</div>

您可能想查找下面的文章:

  • python统计cpu利用率的方法
  • python获取当前计算机cpu数量的方法
  • Python获取单个程序CPU使用情况趋势图
  • 使用python获取CPU和内存信息的思路与实现(linux系统)

相关文章

  • Python中的日期时间处理详解
  • Python爬取网页中的图片(搜狗图片)详解
  • python根据距离和时长计算配速示例
  • Python中的yield浅析
  • Python开发WebService系列教程之REST,web.py,eurasia,Django
  • Python之Web框架Django项目搭建全过程
  • 在Python中操作文件之seek()方法的使用教程
  • python代码制作configure文件示例
  • python删除过期文件的方法
  • Python的函数的一些高阶特性

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • 以Flask为例讲解Python的框架的使用方法
    • python复制与引用用法分析
    • Python中getattr函数和hasattr函数作用详解
    • python paramiko实现ssh远程访问的方法
    • Python fileinput模块使用实例
    • 解读Python编程中的命名空间与作用域
    • bpython 功能强大的Python shell
    • python获取指定目录下所有文件名列表的方法
    • Python中的pass语句使用方法讲解
    • Python解决鸡兔同笼问题的方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有