2

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.

2
  • could you post entire listing with CoInitialize
    – Dewfy
    May 23, 2010 at 12:20
  • No problem, I get on that when I get home.
    – Anders
    May 23, 2010 at 12:25

2 Answers 2

6

Add "sys.coinit_flags = 0" after your "import sys" line and before the "import pythoncom" line. That worked for me, although I don't know why.

3
  • I gotta look that up. Thanks for the advice.
    – Anders
    Sep 10, 2010 at 7:22
  • 2
    0 == pythoncom.COINIT_MULTITHREADED ; This forces the com threading model to multithreaded, which is an implied requirement if you're working with workerthreads. Nov 3, 2011 at 13:55
  • Ondergetekende, how to use it? Where to put? Dec 1, 2020 at 11:59
4
+250

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.

1
  • Good advice, I get back to this thread with more information, thanks for the reply.
    – Anders
    May 26, 2010 at 11:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.