python統(tǒng)計(jì)cpu利用率的方法
本文實(shí)例講述了python統(tǒng)計(jì)cpu利用率的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
#-*-coding=utf-8-*- import win32pdh import time # Counter paths PROCESSOR_PERCENT = r'\Processor(_Total)\% Processor Time' MEMORY_PERCENT = r'\Memory\% Committed Bytes In Use' MEMORY_COMMITTED = r'\Memory\Committed Bytes' PROCESS_BYTES = lambda x: r'\Process(%s)\Private Bytes' % x class Query: def __init__(self): self.counters = {} self.query = None self.query = win32pdh.OpenQuery(None, 0) def add_counter(self, path): if win32pdh.ValidatePath(path) != 0: raise Exception('Invalid path: %s' % path) counter = win32pdh.AddCounter(self.query, path, 0) self.counters[path] = counter def remove_counter(self, path): win32pdh.RemoveCounter(self.counters[path]) del self.counters[path] def get_values(self): values = {} win32pdh.CollectQueryData(self.query) for path in self.counters: status, value = win32pdh.GetFormattedCounterValue( self.counters[path], win32pdh.PDH_FMT_LONG) values[path] = value return values sysinfo_query = Query() sysinfo_query.add_counter(PROCESSOR_PERCENT) sysinfo_query.add_counter(MEMORY_PERCENT) sysinfo_query.get_values() def get_sysinfo(): """Return a tuple (mem_usage, cpu_usage).""" info = sysinfo_query.get_values() return info[MEMORY_PERCENT], info[PROCESSOR_PERCENT] listcpu=[] while True: time.sleep(2) x,y=get_sysinfo() listcpu.append(y) if len(listcpu)==10: icount=0 for c in listcpu: if c>4: icount+=1 if icount>5: print "在統(tǒng)計(jì)的1分鐘內(nèi),cpu已經(jīng)有5次大于4%" listcpu=[] print y
希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。
- Python統(tǒng)計(jì)列表中的重復(fù)項(xiàng)出現(xiàn)的次數(shù)的方法
- python實(shí)現(xiàn)簡單中文詞頻統(tǒng)計(jì)示例
- Python實(shí)現(xiàn)統(tǒng)計(jì)單詞出現(xiàn)的個(gè)數(shù)
- Python實(shí)現(xiàn)對(duì)excel文件列表值進(jìn)行統(tǒng)計(jì)的方法
- python統(tǒng)計(jì)一個(gè)文本中重復(fù)行數(shù)的方法
- 用Python中的字典來處理索引統(tǒng)計(jì)的方法
- Python中統(tǒng)計(jì)函數(shù)運(yùn)行耗時(shí)的方法
- python數(shù)據(jù)結(jié)構(gòu)之二叉樹的統(tǒng)計(jì)與轉(zhuǎn)換實(shí)例
- python高斯分布概率密度函數(shù)的使用詳解
- python實(shí)現(xiàn)beta分布概率密度函數(shù)的方法
- Python實(shí)現(xiàn)的概率分布運(yùn)算操作示例
- Python統(tǒng)計(jì)分析模塊statistics用法示例
相關(guān)文章
python實(shí)現(xiàn)多進(jìn)程并發(fā)控制Semaphore與互斥鎖LOCK
本文主要介紹了python實(shí)現(xiàn)多進(jìn)程并發(fā)控制Semaphore與互斥鎖LOCK,通過實(shí)例來介紹互斥鎖和進(jìn)程并發(fā)控制 semaphore的具體使用,感興趣的同學(xué)可以了解一下2021-05-05深入理解Python虛擬機(jī)中字典(dict)的實(shí)現(xiàn)原理及源碼剖析
這篇文章主要介紹了在?cpython?當(dāng)中字典的實(shí)現(xiàn)原理,在本篇文章當(dāng)中主要介紹在早期?python3?當(dāng)中的版本字典的實(shí)現(xiàn),現(xiàn)在的字典做了部分優(yōu)化,希望對(duì)大家有所幫助2023-03-03Pyinstaller打包.py生成.exe的方法和報(bào)錯(cuò)總結(jié)
今天小編就為大家分享一篇關(guān)于Pyinstaller打包.py生成.exe的方法和報(bào)錯(cuò)總結(jié),小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2019-04-04Python連接數(shù)據(jù)庫使用matplotlib畫柱形圖
這篇文章主要介紹了Python連接數(shù)據(jù)庫使用matplotlib畫柱形圖,文章通過實(shí)例展開對(duì)主題的相關(guān)介紹。具有一定的知識(shí)參考價(jià)值性,感興趣的小伙伴可以參考一下2022-06-06python神經(jīng)網(wǎng)絡(luò)MobileNetV3?small模型的復(fù)現(xiàn)詳解
這篇文章主要為大家介紹了python神經(jīng)網(wǎng)絡(luò)MobileNetV3?small模型的復(fù)現(xiàn)詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-05-05通過Python腳本+Jenkins實(shí)現(xiàn)項(xiàng)目重啟
Jenkins是一個(gè)流行的開源自動(dòng)化服務(wù)器,用于快速構(gòu)建、測(cè)試和部署軟件,本文主要介紹了通過Python腳本+Jenkins實(shí)現(xiàn)項(xiàng)目重啟,具有一定的參考價(jià)值,感興趣的可以了解一下2023-10-10pytorch如何使用訓(xùn)練好的模型預(yù)測(cè)新數(shù)據(jù)
這篇文章主要介紹了pytorch如何使用訓(xùn)練好的模型預(yù)測(cè)新數(shù)據(jù)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-06-06