Importing WMI in a child Thread throws an error

kyosohma at gmail.com kyosohma at gmail.com
Thu Mar 15 20:59:45 CET 2007


On Feb 28, 3:08 pm, Tim Golden <m... at timgolden.me.uk> wrote:
> kyoso... 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

Sorry I didn't reply right away. Here's the straight WMI code I'm
using:

c = wmi.WMI()
    for i in c.Win32_ComputerSystem():
        mem = int(i.TotalPhysicalMemory)
        compname = i.Name
for i in c.Win32_Processor ():
        cputype = i.Name

This code was wrapped in your CoInitialize com objects.

Let me know if you need more code.

Mike




More information about the Python-list mailing list