本文介绍了“发生Win32异常,在...处释放IUnknown".使用Pylons和WMI时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将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 ,您不必担心这样的线程废话.

解决方案

在我看来,Windows并不喜欢您在可能是临时工作线程(如您所指出)上进行这种工作的方式. /p>

在这种情况下,您将无法正常工作,一种可能的解决方法是稍微重构应用程序,以便始终有一个服务线程在运行,您可以查询该信息,而不是而不是设置一切并按需索取.它甚至可能不必是线程,可能只是一个应用程序启动时设置的实用程序类实例,并通过锁进行保护以防止并发访问.

Im using Pylons in combination with WMI module to do some basic system monitoring of a couple of machines, for POSIX based systems everything is simple - for Windows - not so much.

Doing a request to the Pylons server to get current CPU, however it's not working well, or atleast with the WMI module. First i simply did (something) this:

c = wmi.WMI()
for cpu in c.Win32_Processor():
    value = cpu.LoadPercentage

However, that gave me an error when accessing this module via 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)>

Looking at http://timgolden.me.uk/python/wmi/tutorial.html, i wrapped the code accordingly to the example under the topic "CoInitialize & CoUninitialize", which makes the code work, but it keeps throwing "Win32 exception occurred releasing IUnknown at..."

And then looking at http://mail.python.org/pipermail/python-win32/2007-August/006237.html and the follow up post, trying to follow that - however pythoncom._GetInterfaceCount() is always 20.

Im guessing this is someway related to Pylons spawning worker threads and crap like that, however im kinda lost here, advice would be nice.

Thanks in advance,

Anders

EDIT: If you are doing something similar, don't bother with the WMI module, simply use http://msdn.microsoft.com/en-us/library/aa394531%28VS.85%29.aspx , and you don't have to worry about threads crap like this.

解决方案

To me it sounds like Windows is not enjoying the way you are doing this kind of work on what are probably temporary worker threads (as you point out).

If this is the case, and you can't get things to work, one possible workaround would be to re-factor your application slightly so that there is a service thread running at all times which you can query for this information rather than setting everything up and asking for it on demand. It might not even need to be a thread, perhaps just a utility class instance which you get set up when the application starts, protected with a lock to prevent concurrent access.

这篇关于“发生Win32异常,在...处释放IUnknown".使用Pylons和WMI时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 06:48