python - 使用 Pylons 和 WMI 时出现 "Win32 exception occurred releasing IUnknown at..."错误

标签 python multithreading wmi pylons

我结合使用 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/

相关文章:

c++ - 栅栏实际上如何在 C++ 中工作

powershell - 如何使用Powershell检查硬盘驱动器是eide还是sata

python - 如何从具有关系的表中检索数据 - 多对多 (SQLAlchemy)?

python - Python 中的弃用警告,这里有意义吗?

java - 在java中使用main和junit得到不同的结果

c# - 如何杀死 C# 线程?

powershell - 超时 Get-WMIObject cmdlet

c++ - 如何从 Windows 应用程序检测禁用的网络接口(interface)连接?

Python 对象没有引用者但仍可通过弱引用访问?

python - Django 仅在生产环境中使用私有(private) S3 存储