WMI在Python中查询问题

时间:2010-01-20 22:52:49

标签: python wmi wmi-query

我最近一直在研究Squish测试脚本,并尝试做类似解决方案中描述的内容: Total memory used by Python process?

我的代码中的相关摘录如下:

def measureMemory():
    w = wmi.WMI('.')
    result = w.query("SELECT WorkingSet FROM Win32_PerfRawData_PerfProc_Process WHERE Name=\"some_program\"")
    print result
    for WorkingSet in result:
        print WorkingSet
    subset = result[0]
    print subset['WorkingSet']
    # return result[0]['WorkingSet']

for i in range(50):
    memory = measureMemory()
    if memory:
        # test.passes("%d memory used during undo." % memory)
        print memory

不幸的是,每当我真正尝试运行该东西时,我都会遇到错误,如下所示。

[<_wmi_object: \\USER-PC\root\cimv2:Win32_PerfRawData_PerfProc_Process.Name="some_program">]

instance of Win32_PerfRawData_PerfProc_Process
{
    Name = "some_program";
    WorkingSet = "19386368";
};

Traceback (most recent call last):
  File "C:\Python26\Test scripts\Testify", line 25, in -toplevel-
    memory = measureMemory()
  File "C:\Python26\Test scripts\Testify", line 19, in measureMemory
    print subset['WorkingSet']
  File "C:\Python24\Lib\site-packages\win32com\client\dynamic.py", line 242, in __getitem__
    raise TypeError("This object does not support enumeration")
TypeError: This object does not support enumeration

我不确定为什么这应该抛出一个错误,因为我认为我没有改变我从代码中获取代码的重要内容。 我正在使用Python 2.4.4,如果这很重要,不幸的是我不能真正升级,无论它有多大帮助。

1 个答案:

答案 0 :(得分:2)

WMI语法似乎已从示例中更改。尝试使用subset.WorkingSet而不是子集['WorkingSet']