What Multiplex IO in Python 3

I was motivated to see the code of Shadowsocks when I heard the news that the Shadowsocks has be filtered by the GFW. When I saw the source code I found the select package in the server of the Shadowsocks. It seems to be designed to implement the multiuser server to hold many connection in a server side. So I decided to explore the no-blocking connection and the model of the select

When without the select

When I normally want to write a python to connect or accept. I usually write.

Con

But the question is that the server can’t serve the next client until the client closes the connection. When we should serve many users at the same time other users must wait in line before process their tasks. In a word, it is very insufficient to work in this model. So we need to redesign the model in order to serve more users in a machine. So the select model was created.


A glance of select model

One day someone think out a idea ‘The most time waste in a connection is the transferring delay, so how about we start to manipulate the data when the it is transferred otherwise it should wait in the pool. In that way the select model work more efficiently for it only manipulate the data when it is ready.

Analogy

So there is an analogy the select model in real life. When we order in a KFC or McDonalds restaurant several years ago, we order and the waitress prepare you food right now and the next customers should wait before you get you food, but in a select model when you have ordered you should wait in the waiting zone and other customers can order their food and you wait your food to be ready.  In a nutshell the waitress can serve you when you need service and you should wait in the wait zone when you don’t talk with waitresses while other customers who need service can talk and order food. This model can be very efficient.

Code example

Different OS provide different interface to build the model. As an illustration Here I provide a code with comments to show how code actually work in the Linux using Python.

Here we first register the listening socket to handle the incoming connection. Once we get the socket of the incoming connection we register it to the Kqueue to multiplex the actual data transferring. In this way we can serve many users at the same time.


Conclusion

We should make the most use of the Select Model to sufficiently server user.  It is the Select Model that make the nginx powerful.