error occurs when using wmi module in child thread

C

Chen Houwu

------------------sample code begin-------------------------

import threading

import wmi

def run(*args):
c = wmi.WMI ()
memory=c.Win32_LogicalMemoryConfiguration()[0]
info='Total Virtual Memory: '\
+str(int(memory.TotalVirtualMemory)/1024)\
+'MB'
print info

print "Begin run() in main thread"
run()

print "Begin run() in child thread"
thread=threading.Thread(target=run)
thread.start()


------------------sample code end---------------------------

------------------debug info begin--------------------------

Begin run() in main thread
Total Virtual Memory: 1375MB
Begin run() in child thread
Exception in thread Thread-1:
Traceback (most recent call last):
File "C:\my\Python24\lib\threading.py", line 444, in __bootstrap
self.run()
File "C:\my\Python24\lib\threading.py", line 424, in run
self.__target(*self.__args, **self.__kwargs)
File "F:\chen\code\SoftRepairer\script\test\test.py", line 6, in run
c = wmi.WMI ()
File "C:\my\Python24\Lib\site-packages\wmi.py", line 971, in connect
handle_com_error (error_info)
File "C:\my\Python24\Lib\site-packages\wmi.py", line 219, in
handle_com_error
raise x_wmi, "\n".join (exception_string)
x_wmi: -0x7ffbfe1c - Invalid syntax

------------------debug info end----------------------------

------------------info I have found-------------------------

exception raised at:
file: Python24\Lib\site-packages\win32com\client\__init__.py
line: 88
code: moniker, i, bindCtx = pythoncom.MkParseDisplayName(Pathname)
context:

def Moniker(Pathname, clsctx = pythoncom.CLSCTX_ALL):
"""
Python friendly version of GetObject's moniker functionality.
"""
moniker, i, bindCtx = pythoncom.MkParseDisplayName(Pathname)
dispatch = moniker.BindToObject(bindCtx, None,
pythoncom.IID_IDispatch)
return __WrapDispatch(dispatch, Pathname, clsctx = clsctx)

I have compaired the 'Pathname' parameter between main thread and child
thread
when running, they are the same.

-----------------------end----------------------------------

How can I solve the problem?
Thanks
 
P

placid

Chen said:
------------------sample code begin-------------------------

import threading

import wmi

def run(*args):
c = wmi.WMI ()
memory=c.Win32_LogicalMemoryConfiguration()[0]
info='Total Virtual Memory: '\
+str(int(memory.TotalVirtualMemory)/1024)\
+'MB'
print info

print "Begin run() in main thread"
run()

print "Begin run() in child thread"
thread=threading.Thread(target=run)
thread.start()

I had the same problem and wmi module author (Tim Golden) gave me the
solution which;


import pythoncom

# call this function after the run function definition
pythoncom.CoInitialize()


So your function becomes;

def run(*args):
pythoncom.CoInitialize()
c = wmi.WMI ()
memory=c.Win32_LogicalMemoryConfiguration()[0]
info='Total Virtual Memory: '\
+str(int(memory.TotalVirtualMemory)/1024)\
+'MB'
print info



-- Cheers
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,812
Latest member
GracielaWa

Latest Threads

Top