Search code examples
pythonpython-asynciosocketserver

Which Python library should I use? SocketServer or Asyncio?


I am going to create web server which could receive a lot of connections. These 10000 connected users will send to server numbers and server will return these squared numbers to users back. 10000 connections are too many and asynchronous approach is appropriate here. I found two libraries for Python 3.4 which can help:

socketserver & asyncio

With socketserver library we can use ThreadingMixIn and ForkingMixIn classes as async handlers. But this is restricted by number of cores. On the other hand we have asyncio library. And I don't understand how exactly does it works. Which one should I use? And could these two libraries work together?


Solution

  • There are different approaches to asynchronous programming.

    The first approach is to monitor IO operations using threads, and manage those operations in a non-blocking manner. This is what SocketServer does.

    The second approach is to monitor IO operations in the main thread using an event loop and a selector. This is usually what people mean when they talk about asynchronous programming, and that's what asyncio, twisted and gevent do.

    The single-threaded approach has two advantages:

    • it limits the risk of race condition since the callbacks are running in the same thread
    • it gets rid of the overhead of creating one thread per client (see the 10K problem)

    Here is an example of an asyncio TCP server. In your case, simply replace the handle_echo coroutine with your own implementation:

    async def handle_client(reader, writer):
        data = await reader.readline()
        result = int(data.decode().strip()) ** 2
        writer.write(str(result)).encode())
        writer.close()
    

    It should easily be able to handle thousands of clients.