com wrapper and threads

mic aldo123 at
Wed Feb 4 10:18:23 CET 2004

I'm playing with using COM object shared among different threads.
Unfortunately I've come into a problem - I don't know how to properly invoke
com object from external thread. Here comes simplified code I'd like to use:

class ComObjectWrapper:
#this class is by all means needed - of course it's code is much more
complicated than just simply wrapping the COM interface
    def __init__(self):
        self.ComObject = Dispatch(somecom)

    def useCom(self, value):

class MainThread:
    def __init__(self):
        self.Object = ComObjectWrapper()

    def runChildThread(self):
        CThread = ChildThread(self)

class ChildThread(Thread):
    def __init__(self, parent):
        self.Parent = parent

    def run(self):
        #This thread has to use some methods from ComObjectWrapper instance
and it's dispatched COM object
        self.Parent.Object.UseCom() # <--- this generates error

I was trying to use different combinations of sys.coinit_flags = 0 and
pythoncom.COINIT_MULTITHREADED but without success up till now. I've even
managed to send to child thread proper com reference by using stream
marshalling (as stated in "Python programming for win32"), but still I don't
want to use COM itself but its "wrapper" class. Any enlightening thoughts?


More information about the Python-list mailing list