[python-win32] The GIL and COM

Brad Johnson Brad.Johnson at ballardtech.com
Wed Jan 21 17:33:34 CET 2009


I have embedded the Python interpreter into a COM application. In this
application, I create worker threads that execute Python code.

I marshal some COM objects that live in the UI thread of the application over to
the worker thread and wrap them with EnsureDispatch() for use in the new thread.

Meanwhile, the UI thread sometimes has work to do with Python. It attempts to
acquire the GIL and releases it when done. However, while it is waiting for the
GIL in the UI thread (when the worker thread has it), it is locked into a
WaitForSingleObject call (some layers below PyGILState_Ensure).

Anyone that uses COM knows that this is not a COM-safe wait, and message pumping
will not continue. In other words, if my worker thread ever wants to marshal a
call back into the UI thread, it will deadlock. I need some way to use a
COM-safe wait for the GIL, such as CoWaitForMultipleHandles.

Has anyone run into this problem or have a solution?



More information about the python-win32 mailing list