Python 中的 WMI 查询问题

发布于 2024-08-19 07:32:10 字数 1484 浏览 4 评论 0原文

我最近一直在编写 Squish 测试脚本,并尝试执行类似于解决方案中描述的操作: Python进程使用的总内存?

我的代码中的相关片段如下:

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,如果这很重要的话,不幸的是我无法真正升级,无论它有多大帮助。

I've recently been working on a Squish test script, and trying to do something like what's described in the solution at:
Total memory used by Python process?

The relevant snippets from my code are as follows:

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

Unfortunately, I've run into an error whenever I actually try to run the thing, as can be seen below.

[<_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

I'm not sure why this should be throwing an error, as I don't think I've changed anything significant from the example I took code from.
I'm using Python 2.4.4, if that's significant, and unfortunately I can't really upgrade, no matter how much it might help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

開玄 2024-08-26 07:32:10

WMI 语法似乎与示例有所不同。尝试使用subset.WorkingSet而不是subset['WorkingSet']

The WMI syntax seems to have changed from the examples. Try using subset.WorkingSet instead of subset['WorkingSet']

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文