#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