[python-win32] COM server with events and multiple threads

David Janssens dja at info.ucl.ac.be
Fri Jun 3 16:35:30 CEST 2005


I made some changes to the sample and now it seems to work, all the 
events are handled in the main VB6 GUI thread.
The new sample code is attached as a zip file.

I defined a subclass of win32com.server.connect.ConnectableServer.
The difference is that _BroadcastNotify can be called from any thread. 
The actual notifications are made by a separate thread which is 
synchronized with the other threads using queues. The thread that does 
the notifications has a marshalled copy of all the interfaces in 
self.connections.

It works for me, but please let me know if you have other suggestions.

Thanks,
David

Mark Hammond wrote:

>>Thanks for the hints, I will try to modify the sample now to make it
>>work using inter thread marshalling.
>>
>>But isn't it possible to have all the threads in the python
>>COM server run in a single MTA?
>>    
>>
>
>Not when the hosting application (VB in this case) is not in the MTA.  You
>either need to convince VB to live in the MTA (which we have already agreed
>you can't), or live with it <wink>.
>
>  
>
>>Instead of having several worker threads in several STA's in
>>the python
>>COM server with marshalling between the STA's.
>>    
>>
>
>The threads you create are under your control, yes.  So assuming you arrange
>for all threads to be in the MTA, you can freely share objects between the
>threads.  You can't share the object with any threads not in the MTA
>though - which includes the (one and only) VB thread that is not in the MTA.
>
>In this specific case, the problem is we store the object/pointer,
>unmarshalled, in the instance.  If we could store the marshalled object, all
>other threads would be fine.  I'm not sure now to simply get the MTA
>marshalled object in a non-MTA thread, but it may be possible.  Worst case
>you could spin a new thread just to marshall it!
>
>  
>
>>This way, I don't need to implement marshalling between
>>threads in the
>>python COM server and the marshalling could be done
>>implicitely between
>>the python COM server (created in the MTA) and the VB6 gui
>>(running in
>>an STA).
>>    
>>
>
>Yes, that is fine - just not for the VB thread.  VB will only have one
>thread - so in effect, your "main thread" is not in the free-threaded
>apartment, but all other threads can be.
>
>Mark.
>
>  
>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: vbtest.zip
Type: application/x-zip-compressed
Size: 6776 bytes
Desc: not available
Url : http://mail.python.org/pipermail/python-win32/attachments/20050603/bf6a786f/vbtest.bin


More information about the Python-win32 mailing list