error occurs when using wmi module in child thread

placid Bulkan at gmail.com
Tue Jul 11 09:19:34 EDT 2006


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




More information about the Python-list mailing list