1

如何使用 Win32_Processor 类监视 LoadPercentage 更改事件?

import wmi
c= wmi.WMI()
x = [cpu.LoadPercentage for cpu in c.Win32_Processor()]

应该在哪里应用 watch for() 方法,以便我可以知道 CPU 使用率是否已降至低于 80%?

谢谢。湿婆

4

2 回答 2

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。

于 2011-02-22T16:03:29.490 回答
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
于 2011-02-22T16:17:41.010 回答