[python-win32] DispatchWithEvents

Mark Hammond mhammond at skippinet.com.au
Thu May 5 01:51:55 CEST 2011


[re-added the mailing list - please keep everything there]

On 4/05/2011 6:09 PM, Michael Illgner wrote:
> 2011/5/4 Mark Hammond<mhammond at skippinet.com.au>:
>> On 4/05/2011 12:39 AM, Michael Illgner wrote:
>>>
>
>> If everything is happening in a free-threaded context though, no message
>> loop is generally required.
>
> my server "architecture" is quiet simple
>
> com_object = win32com.client.DispatchWithEvents("xyz.xyz", MyEventHandler)
> tcp_handler = MyTCPHandler(com_object)
> server = ThreadedTCPServer((host,port), tcp_handler)
> server.serve_forever()

The question is more about the object xyz.xyz - if it is marked in the 
registry and either free-threading or "both", then you can arrange to 
set sys.coinit_flags=0 (which is COINIT_MULTITHREADED), you should be 
good to go - the objects can probably be used across threads without 
complication.

The threads do force some complication - the com_object can't be used on 
any other thread directly - you must pass it to a different thread via 
pythoncom.CoMarshalInterThreadInterfaceInStream - you will probably need 
to search for samples.  I believe that depending on the threading model, 
the object returned will either marshal back the main thread or just 
make the call directly on the new thread depending on all the threading 
models involved.

To be quite honest, I'm really not sure what will happen to the incoming 
events though - I'm pretty sure no thread marshalling will occur and the 
events will be called directly on the thread used by the COM object - so 
in your example, the threaded tcp server handlers will never see the 
events on its own thread.  Thus, how your main thread needs to stay 
alive will depend on the implementation of xyz.xyz - if it can send 
events while it's main thread isn't running (ie, after it has returned 
from a call you made on it), then it presumably will want to use a 
message loop to dispatch them.

> I have some experience in writing messages loops in C, but can you
> provide me with an example in python ?

pythoncom.PumpMessages() or a loop using pythoncom.PumpWaitingMessages 
is all you need.  However, that will be complicated in your model, as 
the main thread will be in the TCP server's listen loop.  You probably 
need the creation of the server to be in its own thread, which would be 
dedicates to the listen and spawning new threads for handlers.

>> If you want every object to wind up calling the same "handler", then yeah,
>> you would have your event handler delegate to some other singleton.
>>
> My handler needs some configuration data like  an inet adress to send
> data back to the client, my first idea was to pass this data to the
> constructor / __init__ method, but i can use a kind of static or
> global configuration data object for this purpose too.

So in effect you are just "broadcasting" on the tcp-server in response 
to the COM events?  I expect you will wind up needing a queue per 
connection, with each COM event sticking stuff in the queue and the tcp 
handlers reading from one.

Cheers,

Mark


More information about the python-win32 mailing list