problem querying WMI in a background thread

Tim Golden tim.golden at viacom-outdoor.co.uk
Tue Nov 2 03:46:20 EST 2004


[dr_burrito at yahoo.com]
| I'm using Tim Golden's WMI implementation
| (http://tgolden.sc.sabren.com/python/wmi.html) version 0.6 along with
| pywin32 build 202.  I'm able to successfully query WMI in the main
| thread but not in a separate thread.
| 
| Here's a little snippet of sample code that fails for me:

[... snip example ...]

Well, I'm going to be mildly unhelpful, and say that I
don't strictly understand why your code doesn't work.
The thing which usually bites when using WMI and threads
is the CoInitialize trap, but it usually comes out as
an explicit "Must call CoInitialize" exception rather
than as a random COM error. At the end of the day, the
plumbing in WMI is just COM/DCOM so most things comes
down to issues of COM and threading.

But, to me more helpful, the example below does work,
so maybe you can work backwards from it.

<code>
import pythoncom
import wmi
import threading

class Info (threading.Thread):
  def __init__ (self):
    threading.Thread.__init__ (self)

  def run (self):
    print 'In Another Thread...'
    pythoncom.CoInitialize ()
    c = wmi.WMI ()
    while 1:
      for process in c.Win32_Process ():
        print process.ProcessId, process.Name

if __name__ == '__main__':
  print 'In Main Thread'
  c = wmi.WMI ()
  for process in c.Win32_Process ():
    print process.ProcessId, process.Name
  Info ().start ()
</code>

TJG

________________________________________________________________________
This e-mail has been scanned for all viruses by Star. The
service is powered by MessageLabs. For more information on a proactive
anti-virus service working around the clock, around the globe, visit:
http://www.star.net.uk
________________________________________________________________________



More information about the Python-list mailing list