Importing WMI in a child Thread throws an error

Tim Golden mail at timgolden.me.uk
Wed Feb 28 15:08:16 EST 2007


kyosohma at gmail.com wrote:
> On Feb 27, 3:32 pm, Tim Golden <m... at timgolden.me.uk> wrote:
>> kyoso... at gmail.com wrote:
>>> The problem I have is that since I import WMI, it takes a long time
>>> and we have users complaining about it. So I stuck the import
>>> statement into a separate thread and set it to a daemon so it could do
>>> its thing in the background and the rest of the script would finish
>>> and exit.
>> Two things:
>>
>> 1) If you run WMI in a thread, you'll need to call
>> pythoncom.CoInitialize first:
>>
>> <code>
>> import pythoncom
>> import wmi
>>
>> pythoncom.CoInitialize ()
>> c = wmi.WMI ()
>> #
>> # do things
>> #
>> pythoncom.CoUninitialize ()
>> </code>
>>
>> 2) If you need a bit of speed running WMI, see the post
>> I sent a few days ago to someone else:
>>
>> http://mail.python.org/pipermail/python-win32/2007-February/005550.html
>>
>> TJG
> 
> Thanks! This works for my problem. It appears to cut the real time
> required for my script to run by 30-50%. I tried to figure out how to
> apply your answer to the other fellow, but I am actually querying WMI
> for the amount of RAM and the CPU type and I just don't see how to use
> your example in these cases. I am new to the WMI paradigm.

If you want to post some specific code examples, I'm
happy to talk you through possible optimisations.

TJG



More information about the Python-list mailing list