Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python scalable chat server

I've just begun learning sockets with Python. So I've written some examples of chat servers and clients. Most of what I've seen on the internet seems to use threading module for (asynchronous) handling of clients' connections to server. I do understand that for scalable server you need to use some additional tricks, because thousands of threads can kill the server (correct me if I'm wrong, but is it due to GIL?), but that's not my concern at the moment.

The strange thing is that I've found somewhere in Python documentation that creating subprocesses is the right way (unfortunelty I've lost the reference, sorry :( ) for handling sockets.

So the question is: to use threading or multiprocessing? Or is there even better solution?

Please, give me the answer and explain the difference to me.

By the way: I do know that there are things like Twisted which are well-written. I'm not looking for pre-made scalable server, I am instead trying to understand how to write one that can be scaled or will deal with at least 10k clients.

EDIT: The operating system is Linux.

like image 470
freakish Avatar asked Oct 21 '11 08:10

freakish


Video Answer


1 Answers

Facebook needed a scalable server so they wrote Tornado (which uses async). Twisted is also famously scalable (it also uses async). Gunicorn is also a top performer (it uses multiple processes). None of the fast, scalable tools that I know about uses threading.

An easy way to experiment with the different approaches is to start with the SocketServer module in the standard library: http://docs.python.org/library/socketserver.html . It lets you easily switch approaches by alternately inheriting from either ThreadingMixin or ForkingMixin.

Also, if you're interested in learning about the async approach, the easiest way to build your understanding is to read a blog post discussing the implementation of Tornado: http://golubenco.org/2009/09/19/understanding-the-code-inside-tornado-the-asynchronous-web-server-powering-friendfeed/

Good luck and happy computing :-)

like image 200
Raymond Hettinger Avatar answered Nov 15 '22 16:11

Raymond Hettinger