[Twisted-Python] Creating a COM object in a thread

Don Dwiggins ddwiggins at advpubtech.com
Mon Jul 5 21:26:08 EDT 2010

> The way I do this is I isolate all the COM stuff in a separate thread
> from thread that runs my reactor.

This all looks good, and I think I understand it.  One question, though: 
from my investigation, it seems that the hangup is in the COM object 
creation itself, where Mark and I think it's waiting on a Windows 
message.  Wouldn't I have to have the event loop running before calling 
Dispatch?  (FWIW, I do call CoInitializeEx the same way you do, and have 
the object built with apartment threading.)

> This thread starts by calling:
>      pythoncom.CoInitializeEx(pythoncom.COINIT_APARTMENTTHREADED)
> it then creates the COM object and runs an event loop something like this:
>      while keepRunning:
>          n = MsgWaitForMultipleObjects(handles, False, timeout, QS_ALLINPUT)
>          if n == WAIT_OBJECT_0 + nHandles:
>              # There is a win32 message waiting.
>              if pythoncom.PumpWaitingMessages():
>                  keepRunning = False
> I send messages to the thread by putting them in a queue and setting
> an event.  The messages cause methods to be called on the COM object.
> I send the results of the COM methods back to the reactor thread using
> reactor.callFromThread().

Thanks much for the help,

Don Dwiggins
Advanced Publishing Technology

More information about the Twisted-Python mailing list