[python-win32] COM server with events and multiple threads
Mark Hammond
mhammond at skippinet.com.au
Wed May 18 03:30:53 CEST 2005
> I am trying to build an application where the GUI is implemented as a
> VB6 exe and most of the core functionality is implemented as a python
> COM server that is called by the GUI.
>
> The python COM server has multiple threads running in it and sends
> events once in a while to the VB6 GUI.
>
> The python COM server needs to be run in the MTA of the application's
> process because all the events from the server to the GUI need to be
> marshalled, so I set the threading model of the python COM
> server to "free".
A COM server can only indicate the threading options it supports - the
*creator* of the COM object is responsible for setting the actual thread
mode used. If the server doesn't support the mode, you get that error.
Consider: The thread mode is set by a CoInitialize(Ex) call. Such a call
must be made before your COM object can be used or instantiated by that
thread. Therefore, by the time your server has been created/called, the
thread mode must have already been set.
You need to tell VB to use free-threading if you want your object to be only
used that way.
> disappears. But of course that doesn't suit me because then
> some VB6 gui
> code that handles COM server events gets called in threads that are
> different from the main gui thread.
COM itself should take care of that. Earlier you mentioned:
> The python COM server has multiple threads running in it and sends
> events once in a while to the VB6 GUI.
How do you pass the event sink objects to the threads? If ever a COM
objects crosses thread boundaries, it must do so via
CoMarshalInterThreadInterfaceInStream(). If you fail to do that, then you
will not get COM's proxying. Note however that the call you make in this
thread will still block until the other thread could handle it - ie, the
event delivery is unlikely to be asynch.
Mark
More information about the Python-win32
mailing list