日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:用Python來統(tǒng)計本機(jī)CPU利用率

模塊win32pdh是python中的一個模塊,封裝了Windows Performance Data Helpers API。

專注于為中小企業(yè)提供網(wǎng)站制作、成都做網(wǎng)站服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)黑河免費(fèi)做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了數(shù)千家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。

win32pdh方法

AddCounter    添加一個新計數(shù)器

AddEnglishCounter    通過英文名稱為查詢添加計數(shù)器

RemoveCounter    刪除一個打開的計數(shù)器。

EnumObjectItems    枚舉對象的項目

EnumObjects    枚舉對象

OPENQUERY    打開一個新查詢

CloseQuery    關(guān)閉打開的查詢。

MakeCounterPath    制作完全解決的計數(shù)器路徑

GetCounterInfo    檢索有關(guān)計數(shù)器的信息,例如數(shù)據(jù)大小,計數(shù)器類型,路徑和用戶提供的數(shù)據(jù)值。

GetFormattedCounterValue    檢索格式化的計數(shù)器值

CollectQueryData    收集指定查詢中所有計數(shù)器的當(dāng)前原始數(shù)據(jù)值,并更新每個計數(shù)器的狀態(tài)代碼。

ValidatePath    驗證指定的計數(shù)器是否存在于計數(shù)器路徑中指定的計算機(jī)上。

ExpandCounterPath    檢查指定的計算機(jī)(如果沒有指定本地計算機(jī)),則檢查與計數(shù)器路徑中的通配符字符串匹配的計數(shù)器和計數(shù)器實例。

ParseCounterPath    解析計數(shù)器路徑的元素。

ParseInstanceName    解析實例名稱的元素

SetCounterScaleFactor    設(shè)置在請求格式化計數(shù)器值時應(yīng)用于指定計數(shù)器的計算值的比例因子。

BrowseCounters    顯示計數(shù)器瀏覽對話框,以便用戶可以選擇要返回給調(diào)用者的計數(shù)器。

ConnectMachine    連接到指定的計算機(jī),并在PDH DLL中創(chuàng)建和初始化計算機(jī)條目。

LookupPerfIndexByName    返回與指定計數(shù)器名稱對應(yīng)的計數(shù)器索引。

LookupPerfNameByIndex    返回與指定索引對應(yīng)的性能對象名稱。

完整代碼:python統(tǒng)計cpu利用率

#-*-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)計的1分鐘內(nèi),cpu已經(jīng)有5次大于4%"
    listcpu=[]
  print y


分享標(biāo)題:創(chuàng)新互聯(lián)Python教程:用Python來統(tǒng)計本機(jī)CPU利用率
網(wǎng)頁鏈接:http://www.5511xx.com/article/dhhhhjg.html