我将 Pylons 与 WMI 模块结合使用来对几台机器进行一些基本的系统监控,对于基于 POSIX 的系统,一切都很简单 - 对于 Windows - 不是那么简单。
向 Pylons 服务器发出请求以获取当前 CPU,但是它不能正常工作,或者至少对于 WMI 模块来说是这样。首先我只是做了(某事):
c = wmi.WMI()
for cpu in c.Win32_Processor():
value = cpu.LoadPercentage
然而,当我通过 Pylons 访问这个模块时,这给了我一个错误(GET http://ip:端口/cpu 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 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 http://mail.python.org/pipermail/python-win32/2007-August/006237.html以及后续帖子,试图遵循这一点 - 然而pythoncom._GetInterfaceCount()
始终为 20。
我猜这与塔产生工作线程和类似的废话有某种关系,但是我有点迷失在这里,建议会很好。
提前致谢,
Anders
编辑:如果您正在做类似的事情,请不要使用 WMI 模块,只需使用http://msdn.microsoft.com/en-us/library/aa394531%28VS.85%29.aspx http://msdn.microsoft.com/en-us/library/aa394531%28VS.85%29.aspx,你不必担心像这样的线程垃圾。