asyncore, asynchat and threads

Joshua Moore-Oliva josh at chatgris.com
Wed Aug 4 04:08:36 CEST 2004


> The usual approach is to use something along the lines of a separate
> thread for the *work* rather than for the connection, or maybe a
> thread pool, and communicate with those threads from the asynchat
> thingies using Queue.Queues...
> 
> I'm not sure we have enough information about what you really need
> to say more.  You've given lots of info about what you "would love
> to do" but not enough about what you actually need to do. <wink>
> 
> -Peter

Ok, I will give more info, and also, you touched on a subject that I thought was impossible for my problem domain, but here goes.

I have a very simple protocol setup as follows..  basically it is a method for retrieving data, however I must route data through a central
connection to avoid database locking.

A simplified version of my transaction looks like so..
 '>' means data from the client, '<' means data from the server
> BATCH_GET
< 1

Right after the BATCH_GET I wait on a Queue object for data to send to the remote client.  
Depending on whether there is data available for this specific connection, this call could never return, but other connections
could also be requesting data, and there could be data available to them.

I currently do have a separate thread doing all the work, and it puts each threads data into a separate queue.

The problem that I ran into with using a direct asyncore//asynchat interface is this... (hoping there is a way to work around it)

consider the very likely possiblity that asynchat retrieves the line BATCH_GET, but there is no data to return.

I have to not return any data until there is data to return for that specific connection.  If there is some way to send data down a asynchat
socket from another thread (thread-safely), or to somehow wake up an asynchat class, that would be great.

The problem is that once BATCH_GET is received, unless i handle it there and then, no more events will be generated as the remote client will
be waiting for a response.  

Hopefully this sufficiently answers your 'what I actually *need* to do' question ;)

I don't want to flood the email with a lot of irrelevant info, so if you would like more infomation, ask away, but I think this covers the problem sufficiently...

Joshua Moore-Oliva




More information about the Python-list mailing list