Advice on sockets and threads

Russell E. Owen no at spam.invalid
Wed May 21 15:13:54 EDT 2003


In article <hv7ncvs6tnqjdp379vleok2qun87t1j0s7 at 4ax.com>,
 Gon?alo Rodrigues <op73418 at mail.telepac.pt> wrote:

>...Anyway, while staring at the code, I noticed that this would degenerate
>into a busy loop if there is nothing to read and nothing to write. It
>just doesn't seem the right thing to do. So, how you guys do it? Spawn a
>new thread whose only responsability is standing by the queue, peel what
>data is there and write it to the socket? Or is the above just fine and
>dandy? Some other scheme?

I do think you should use a dedicated thread for writing. Then the code 
becomes simpler (at the expense of an extra thread) because your writing 
thread can safely block while waiting for the next queued message and 
also safely block while writing.

However, you may wish to check out the twisted framework. I've not used 
it personally (yet) but it sounds as if it will do exactly what you want.

-- Russell




More information about the Python-list mailing list