Python opening multiple thread of matlab

Stefan Schukat SSchukat at dspace.de
Sat Nov 11 16:15:02 EST 2006


Hello, 

you just forgot to initialize the COM runtime for the separate thread. 
try following:

    def __init__(self,matlab_command):
        self.matlab_command = matlab_command
        threading.Thread.__init__(self)
 
    def run(self):
	  import pythoncom
	  pythoncom.CoInitialize()
        try:
		matlab_object = Dispatch('matlab.application.single')
            execute = getattr(matlab_object,'Execute')
            execute(self.matlab_command)
        finally:
            matlab_object = None
            pythoncom.CoUnitialize()


	Stefan
 

> -----Original Message-----
> From: python-list-bounces+sschukat=dspace.de at python.org 
> [mailto:python-list-bounces+sschukat=dspace.de at python.org] On 
> Behalf Of tsjuan
> Sent: Saturday, November 11, 2006 2:56 PM
> To: python-list at python.org
> Subject: Python opening multiple thread of matlab
> 
> Hello Python Users,
> 
> I've been trying to run multiple thread of Matlab by calling 
> its com object via python. However, I keep getting error 
> message that says Python can't find the attribute of certain 
> function that I want to execute in Matlab.
> 
> I know the com function is exist, it works just fine if I 
> don't run within thread.
> Below is my sample code, any helps or comments are appreciated.
> 
> Thanks,
> Tanto
> 
> import threading
> from win32com.client import Dispatch
> 
> 
> class MyThread ( threading.Thread ):
> 
>    def __init__(self,matlab_command):
>        self.matlab_command = matlab_command
>        self.matlab_object = Dispatch('matlab.application.single')
>        threading.Thread.__init__(self)
> 
>    def run(self):
>        execute = getattr(self.matlab_object,'Execute')
>        execute(self.matlab_command)
> 
>    def awesome_dud(self):
>        execute = getattr(self.matlab_object,'Execute')
>        execute(self.matlab_command)
> 
> 
> a = MyThread('a=1:1:100')
> b = MyThread('b=1:1:200')
> 
> # Running matlab function through thread (It's not working) # 
> =========================================================
> 
> a.start()
> b.start()
> a.join()
> b.join()
> 
> # Running matlab function not through thread (it's working) # 
> =========================================================
> a.awesome_dud()
> b.awesome_dud()
> 
> --
> http://mail.python.org/mailman/listinfo/python-list
> 



More information about the Python-list mailing list