By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,538 Members | 1,293 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,538 IT Pros & Developers. It's quick & easy.

error occurs when using wmi module in child thread

P: n/a
------------------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

Jul 11 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a

Chen Houwu wrote:
------------------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

Jul 11 '06 #2

P: n/a
thanks to solve my big problem :-)
I will try it later.

Jul 11 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.