我结合使用 Pylons 和 WMI 模块对几台机器进行一些基本的系统监控,对于基于 POSIX 的系统,一切都很简单 - 对于 Windows - 不是那么多。
向 Pylons 服务器发出请求以获取当前 CPU,但它运行不正常,或者至少与 WMI 模块不兼容。首先,我只是做了(某事)这个:
c = wmi.WMI()
for cpu in c.Win32_Processor():
value = cpu.LoadPercentage
但是,当我通过 Pylons 访问此模块时出现错误 (GET http://ip:port/cpu ):
raise x_wmi_uninitialised_thread ("WMI returned a syntax error: you're probably running inside a thread without first calling pythoncom.CoInitialize[Ex]")
x_wmi_uninitialised_thread: <x_wmi: WMI returned a syntax error: you're probably running inside a thread without first calling pythoncom.CoInitialize[Ex] (no underlying exception)>
查看http://timgolden.me.uk/python/wmi/tutorial.html ,我根据主题“CoInitialize & CoUninitialize”下的示例包装了代码,这使得代码可以工作,但它一直抛出 “Win32 exception occurred releasing IUnknown at...”
然后查看http://mail.python.org/pipermail/python-win32/2007-August/006237.html以及后续帖子,试图遵循这一点 - 然而 pythoncom._GetInterfaceCount()
始终为 20。
我猜这在某种程度上与 Pylons 产生工作线程和类似的废话有关,但是我有点迷失在这里,建议会很好。
提前致谢
安德斯
编辑:如果您正在做类似的事情,请不要理会 WMI 模块,只需使用 http://msdn.microsoft.com/en-us/library/aa394531%28VS.85%29.aspx ,而且您不必担心像这样的线程废话。
最佳答案
在“import sys”行之后和“import pythoncom”行之前添加“sys.coinit_flags = 0”。这对我有用,尽管我不知道为什么。
关于python - 使用 Pylons 和 WMI 时出现 "Win32 exception occurred releasing IUnknown at..."错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2880723/