Importing WMI in a child Thread throws an error

Tim Golden mail at
Thu Mar 15 21:18:19 CET 2007

>> 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

Well, don't know how much gain you'll get, but yo
could try the following quickies:

import wmi
c = wmi.WMI (find_classes=False)

for i in c.Win32_ComputerSystem (
   ['TotalPhysicalMemory', 'Name']
   mem = int (i.TotalPhysicalMemory)
   compnam = i.Name

for i in c.Win32_Processor (['Name']):
   cputype = i.Name


If you were going to repeat these often (say, in
a loop, which doesn't seem likely given you
examples) you might gain a few nanosecs by
pulling the attribute lookup outside the loop:

import wmi
c = wmi.WMI (find_classes=False)

ComputerSystem = c.Win32_ComputerSystem
Processor = c.Win32_Processor

while True:
   for computer_system in ComputerSystem (...): ...
   for processor in Processor (...): ...


But, as everyone else on this list will tell you,
there's no point in optimising unless you know you
need to to, and unless you know where :-) That's
what modules like timeit profiler are for.


More information about the Python-list mailing list