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

Mark Wright markscottwright at gmail.com
Tue Jun 29 10:01:54 EDT 2010


I'm sure you've already checked the simple things like this, but have
you called CoInitialize() in your thread?  If you're using the win32
extensions, it'll be called for you in the main thread, but any new
thread needs to call it before attempting to do any COM work.

On Mon, Jun 28, 2010 at 7:43 PM, Don Dwiggins <ddwiggins at advpubtech.com> wrote:
> 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.
>
> TIA,
> --
> Don Dwiggins
> Advanced Publishing Technology
>
>
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>



-- 
Mark Wright
markscottwright at gmail.com



More information about the Twisted-Python mailing list