如何使用Win32_Processor类观察LoadPercentage更改事件?
import wmi
c= wmi.WMI()
x = [cpu.LoadPercentage for cpu in c.Win32_Processor()]
应该使用watch for()方法在哪里,以便我可以知道CPU使用率是否降至80%以下?
感谢。 希瓦
答案 0 :(得分:1)
我不使用该库,但这是一个示例查询:
from win32com.client import Moniker
wmi = Moniker('winmgmts:')
events = wmi.ExecNotificationQuery("Select * From __InstanceModificationEvent "
"Within 1 "
"Where TargetInstance Isa 'Win32_Processor' "
"And TargetInstance.LoadPercentage > 10")
processor = events.NextEvent().TargetInstance
print processor.LoadPercentage
您也可以尝试使用其中一个perf WMI类而不是Win32_Processor。
答案 1 :(得分:1)
我不确定你对for()方法的意思,但你可以把它放在一个循环中:
kMaxLoad = 80
while True:
x = [cpu.LoadPercentage for cpu in c.Win32_Processor()]
if max(x) < kMaxLoad:
break
print "okay, load is under %i" % kMaxLoad