python实时监控cpu小工具
程序员文章站
2023-01-26 21:29:05
本文实例为大家分享了python实时监控cpu的工具,供大家参考,具体内容如下
虽然写的很不完善,但是当练手吧,对于实时监控cpu还是有点用处的,虽然android st...
本文实例为大家分享了python实时监控cpu的工具,供大家参考,具体内容如下
虽然写的很不完善,但是当练手吧,对于实时监控cpu还是有点用处的,虽然android studio已经提供了一样的功能,但是能够记录并生成成本地csv文件还是比as多一个功能的。
结果图:
直接放源码:
# !_*_coding:utf-8_*_ import re import os import wx import thread import pychartdir import csv class getcpustats(): def __init__(self, name): self.pro_name = name def startwatch(self): _shell = 'adb shell top -s cpu -n 1| findstr %s' % self.pro_name result = os.popen(_shell).read() return self.getinfo(result) def getinfo(self, _result): return [re.findall('(\d+)%', _result), re.findall('k\s(\d+)k', _result)] class myframe(wx.frame): flag = false _label = [] _data = [] _label02 = [] _data02 = [] num = 0 num02 = 0 def __init__(self): wx.frame.__init__(self, parent=none, title=u'cpu检测小工具', pos=(700, 100), size=(500, 500)) mpanel = wx.panel(parent=self) msizer = wx.boxsizer(orient=wx.vertical) btn_start = wx.button(parent=mpanel, label=u'开始', style=wx.expand) msizer.add(btn_start) btn_stop = wx.button(parent=mpanel, label=u'停止', style=wx.expand) msizer.add(btn_stop) self.edit = wx.textctrl(parent=mpanel,style=wx.expand,value=u'这里输入测试的包名') msizer.add(self.edit,flag=wx.expand) self.edit.bind(event=wx.evt_choice, handler=self.requestfocus) self.bitmap_cpu = wx.staticbitmap(parent=mpanel) msizer.add(self.bitmap_cpu, flag=wx.expand) #self.bitmap_mem = wx.staticbitmap(parent=mpanel) #msizer.add(self.bitmap_mem, flag=wx.expand) mpanel.setsizer(msizer) mpanel.fit() btn_start.bind(event=wx.evt_button, handler=self.startwatch) btn_stop.bind(event=wx.evt_button, handler=self.stopwatch) def requestfocus(self,event): self.edit.setvalue("") def startwatch(self, event): self.flag = true thread.start_new_thread(self.getinfo, ()) def stopwatch(self, event): self.flag = false def getinfo(self): while(self.flag): _result = getcpustats(self.edit.getvalue()).startwatch() self.drawpic(_result) #self.drawpic_mem(_result) def writecsv(self, _list): writer = csv.writer(file('tmp.csv', 'a+')) writer.writerow([_list]) def drawpic(self,_list): self.num+=1 self._label.append(str(self.num)) self._data.append(_list[0][0]) self.writecsv(_list[0][0]) c = pychartdir.xychart(500, 400) c.setplotarea(30, 20, 400, 300) c.addlinelayer(self._data) c.xaxis().setlabels(self._label) c.makechart("tmp.png") mimage = wx.image("tmp.png") mimage = mimage.scale(500,300) self.bitmap_cpu.setbitmap(wx.bitmapfromimage(mimage)) def drawpic_mem(self,_list): self.num02+=1 self._label02.append(str(self.num02)) self._data02.append(_list[1][0]) c = pychartdir.xychart(500, 400) c.setplotarea(60, 40, 400, 300) c.addlinelayer(self._data02) c.xaxis().setlabels(self._label02) c.makechart("tmp02.png") mimage = wx.image("tmp02.png") mimage = mimage.scale(500,300) self.bitmap_mem.setbitmap(wx.bitmapfromimage(mimage)) if __name__ == '__main__': app = wx.app() myframe().show() app.mainloop()
原理:
界面用wx做的,图形用pychartdir做的,命令用的是top命令,每次截取到的数据都生成一张临时图片,然后刷新到屏幕上,看起来就是实时数据监控的效果了,基本上bug一大堆,也懒得改了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
推荐阅读