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

Don Dwiggins ddwiggins at advpubtech.com
Mon Jun 28 20:43:14 EDT 2010

I need to create a COM object in a Windows application, and call it. 
Since the call will take some time to execute, I wrap it in a deferToThread.

I've found that, when I create the object inline, it works.  However, 
when I defer it, it hangs up in the win32com.client.Dispatch call.  I've 
tried several things, including digging into the guts of Dispatch -- the 
hangup occurs during the creation of the object.

I've discussed this with Mark Hammond, who suggests one lead:

"The Windows message loop is used by the COM marshalling process.  IIRC, 
the first thread to initialize COM in a process is the thread in which 
single-threaded objects will always end up being called in.  If a 
different thread creates the object, COM uses Windows messages to 
marshall all calls back to that main thread.  IOW, your second thread 
makes a call - even to create the object - which results in that thread 
sending a windows message to the main thread to act on the request.

What this probably means in practice is that twisted needs to use a 
reactor which calls MsgWaitForMultipleObjects() and runs a message loop 
when the function detects a new message is in the queue.  I'm not sure 
if there is an existing reactor which does this."

I'm posting this on the off chance that someone else has wandered into 
this corner of Windows arcana, and has come up with something useful.

I'm about to give up on doing this "inside" the twisted app, and running 
it in a separate app that I'll call from the thread.

Don Dwiggins
Advanced Publishing Technology

More information about the Twisted-Python mailing list