com wrapper and threads

mic aldo123 at onet.pl
Wed Feb 4 04:18:23 EST 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):
            self.ComObject.use(value)

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

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

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
        pythoncom.CoInitialize()
        self.Parent.Object.UseCom() # <--- this generates error
        pythoncom.CoUninitialize()

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?

Michal





More information about the Python-list mailing list