views:

39

answers:

1

What's the best way to handle lots of connections at once in python? The first way I think of is threading, which works but at 10MB of RAM per thread that's rather expensive.

So what other ways are there to handle lots of connections at once ?

The only problem I see without using threads is that using the socket.recv() waits for data from that protocolar client so one thread handling several clients wouldn't work.

But that's why I'm asking this question, whats the best way to handle several connections?

Thank you!

+5  A: 

A good alternative to threads, if you have a truly large amount of simultaneous connections, is an asychronous/events based approach with callbacks.

Python already has a very mature and powerful library/framework for this purpose - Twisted. There's also a simpler solution using the standard library asyncore module. From its docs:

This module provides the basic infrastructure for writing asynchronous socket service clients and servers.

There are only two ways to have a program on a single processor do “more than one thing at a time.” Multi-threaded programming is the simplest and most popular way to do it, but there is another very different technique, that lets you have nearly all the advantages of multi-threading, without actually using multiple threads. It’s really only practical if your program is largely I/O bound. If your program is processor bound, then pre-emptive scheduled threads are probably what you really need. Network servers are rarely processor bound, however.

Generally, asynchronous socket servers is a very hot topic lately (node.js hype as a witness), I'm sure you can find a lot of interesting material online.

Eli Bendersky