Python, COM and Threads

Robert Armes/CAM/Lotus robert_armes at us.ibm.com
Thu Aug 29 13:47:29 EDT 2002




Hi,

I am hoping that someone from this group might be able to help me with the
following problem:

I have embedded and extended python within my win32 application. I would
like to able to run scripts asynchronously as needed. Toward that end, I am
calling PyRun_String (from the c++ side) in order to run my scripts. Then
within the script end of things, I spawn new, dedicated threads, from which
the script attempts to instantiate a com object by calling win32com.client.
Dispatch(...). The problem is that the com object is not successfully
created.

If I remove the layer which creates the new thread and directly instantiate
the com object from the main thread, then all is well. This leads me to
think that the problem is due to the threading apartment model used by com.
Unfortunately, attempting to explicitly change the model by calling sys.
coinit_flags=0 doesn't work.

Any suggestions? In an effort to be as concise as possible, I have
intentionally left out the (hopefully) unnecessary details. I can fill in
the blanks if required.

 - Robert





More information about the Python-list mailing list