您好,登錄后才能下訂單哦!
python中怎么實(shí)時(shí)監(jiān)控cpu,相信很多沒有經(jīng)驗(yàn)的人對(duì)此束手無策,為此本文總結(jié)了問題出現(xiàn)的原因和解決方法,通過這篇文章希望你能解決這個(gè)問題。
源碼:
# !_*_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檢測(cè)小工具', 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'這里輸入測(cè)試的包名') 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()
看完上述內(nèi)容,你們掌握python中怎么實(shí)時(shí)監(jiān)控cpu的方法了嗎?如果還想學(xué)到更多技能或想了解更多相關(guān)內(nèi)容,歡迎關(guān)注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。