您的位置:首页 > 编程语言 > Python开发

python利用wmi模块统计windows下cpu信息

2012-09-03 13:59 330 查看
# -*- 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本文出自 “学海无涯苦作伴” 博客,请务必保留此出处http://linuxshow.blog.51cto.com/1572053/980639
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: