views:

94

answers:

3

Hi, I'm trying to write a python lib that will implement the client side of a certain chat protocol. After i connect to the server i start the main loop where i read from the server and handle received commands and here i need to call a callback function (like on_message, or on file_received, etc). How should i go about implementing this? Should a start a new thread for each callback function? As maybe some callbacks will take some time to return and i will timeout. Also if the main loop where i read from the server is in a thread can i write to the socket from another thread(send messages to the server)?
Or is there a better approach? Thanks.

+5  A: 

For a python app doing this, I wouldn't use threads. I would use a framework like Twisted.

The docs have examples; here's a chat example.

Thomas Vander Stichele
+1  A: 

I would use the select module, or alternately twisted, however select is a bit more portable, and to my mind somewhat more pythonic.

Richo
A: 

Threads are just an unnecessary complication here and will lead to obscure bugs if you're not familiar with how to use them correctly. asyncore or asynchat are simple routes to the same goal, however.

Kylotan