Converting existing module/objects to threads

Jean-Paul Calderone exarkun at divmod.com
Thu Oct 19 13:21:44 EDT 2006


On Thu, 19 Oct 2006 14:09:11 -0300, Gabriel Genellina <gagsl-py at yahoo.com.ar> wrote:
>At Thursday 19/10/2006 00:01, jdlists at gmail.com wrote:
>> > Consider using the asyncore module instead of threads.
>>
>>I think that is a good point and I am considering using
>>asyncore/asynchat...  i'm a little confused as to how i can make this
>>model work.  There is no server communication without connection from
>>the client (me), which happens on intervals, not when data is available
>>on a socket or when the socket is available to be written, which is
>>always.  Basically i need to determine how to trigger the asynchat
>>process based on time.  in another application that i write, i'm the
>>server and the chat process happens every time the client wakes
>>up...easy and perfect for asyncore
>>
>>That is a solution i'd like to persue, but am having a hard time
>>getting my head around that as well.
>
>You have to write your own dispatcher (inheriting from async_chat) as any 
>other protocol. You can call asyncore.loop whith count=1 (or 10, but not 
>None, so it returns after a few iterations) inside your *own* loop. Inside 
>your loop, when time comes, call your_dispatcher.push(data) so the channel 
>gets data to be sent. Override collect_incoming_data() to get the response.
>You can keep your pending requests in a priority queue (sorted by time) and 
>check the current time against the top element's time.

You could also use Twisted, which provides time-based primitives in addition
to supporting network multiplexing without threads.

Jean-Paul



More information about the Python-list mailing list