daniels
daniels

Reputation: 19233

python chat client lib

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.

Upvotes: 0

Views: 508

Answers (3)

Kylotan
Kylotan

Reputation: 18449

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.

Upvotes: 1

richo
richo

Reputation: 8999

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

Upvotes: 2

Thomas Vander Stichele
Thomas Vander Stichele

Reputation: 36549

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.

Upvotes: 6

Related Questions