python利用wmi模塊統計windows下cpu信息

# -*- coding: utf-8 -*-

#import
########################################################################
import os, sys
import time
import wmi
########################################################################
#function
########################################################################
def get_cpu_info() :
         tmpdict = {}
         tmpdict["CpuCores"] = 0
         c = wmi.WMI ()
         for cpu in c.Win32_Processor():            
                 tmpdict["CpuType"] = cpu.Name
                 try:
                         tmpdict["CpuCores"] = cpu.NumberOfCores
                 except:
                         tmpdict["CpuCores"] += 1
                 tmpdict["CpuClock"] = cpu.MaxClockSpeed    
         return tmpdict

def _read_cpu_usage():
         c = wmi.WMI ()
         for cpu in c.Win32_Processor():
                 return cpu.LoadPercentage
            
def get_cpu_usage():
         """
         get cpu avg used by percent
         """
         cpustr1 =_read_cpu_usage()
         if not cpustr1:
                 return 0
         time.sleep(2)
         cpustr2 = _read_cpu_usage()
         if not cpustr2:
                 return 0
         cpuper = int(cpustr1)+int(cpustr2)/2
         return cpuper

if __name__ == "__main__":
         a = get_cpu_info()
         print a
         print '--------------------------'
         b = get_cpu_usage()
         print b
其他python網站訪問地址:http://bbs.pythonfan.org/thread-2348-1-1.html
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章