It seems that the function of this parameter is to limit the number of incoming connection requests that the server will store in the queue, assuming that it can serve the current request and a small number of waiting requests in the queue for a reasonable period of time under high load. Here is a good paragraph that I came across that gives a little context around this argument ...
Finally, the listening argument tells the socket library that we want it to queue up to 5 connection requests (normal maximum) before rejecting external connections. If the rest of the code is written correctly, this should be enough.
https://docs.python.org/3/howto/sockets.html#creating-a-socket
Previously, the document contained text that suggested clients connect and disconnect from the server so that you would not create a long queue of requests in the first place ...
When connect complete, socket s can be used to send a request to the page text. The same socket will read the response and then be destroyed. That's right, destroyed. Client sockets are usually used for only one exchange (or a small set of consecutive exchanges).
The HowTo related guide is a must-have when reading about socket programming for network programming. It really focuses on some of the big picture themes. Now, as the server socket manages this queue, how much implementation details is another story, perhaps interesting. I believe that the motivation for this design is more indicative; without this , a denial of service barrier would be very, very low.
As for the reason for the minimum value of 0 versus 1, we must remember that 0 is still a valid value, i.e. don't queue anything. In essence, this means that there will be no request queue, just reject the connection if the server socket is currently servicing the connection. In this context, you should always remember the service point of the active connection, the only reason why the queue will be interesting in the first place.
This brings us to the next issue of preferred cost. This is all a design decision, do you want to queue requests or not? If so, you can choose a value that, in your opinion, is justified based on expected traffic and known hardware resources, I suppose. I doubt that there is anything formal in choosing a value. This makes me think about how easy the request is in the first place, when you will be fined for posting anything on the server.
UPDATE
I wanted to justify the comments from user207421 and went looking for the source in python. Unfortunately, this level of detail is not in the sockets.py source , but in socketmodule. C # L3351-L3382 with a hash of 530f506.
The comments are very useful, I will copy the verbatim source below and highlight the explanatory comments that quite illuminate ...
We try to select a high enough level of default jobs to avoid connection failures for normal workloads, but not too high to limit resource usage.
and
If the lag is specified, it must be at least 0 (if it is less, it is 0); it indicates the number of unacceptable connections that the system will allow before refusing new connections. If not specified, a reasonable default is selected.
static PyObject * sock_listen(PySocketSockObject *s, PyObject *args) { int backlog = Py_MIN(SOMAXCONN, 128); int res; if (!PyArg_ParseTuple(args, "|i:listen", &backlog)) return NULL; Py_BEGIN_ALLOW_THREADS if (backlog < 0) backlog = 0; res = listen(s->sock_fd, backlog); Py_END_ALLOW_THREADS if (res < 0) return s->errorhandler(); Py_RETURN_NONE; } PyDoc_STRVAR(listen_doc, "listen([backlog])\n\ \n\ Enable a server to accept connections. If backlog is specified, it must be\n\ at least 0 (if it is lower, it is set to 0); it specifies the number of\n\ unaccepted connections that the system will allow before refusing new\n\ connections. If not specified, a default reasonable value is chosen.");
Going further down the rabbit hole into the outer space, I discovered the following source from the socket module ...
res = listen(s->sock_fd, backlog);
This source ends in socket.h and is shortened using Linux as a specific background platform for discussion purposes.
#define SOMAXCONN 128 extern int __sys_listen(int fd, int backlog);
There you can find more information in the help page.
http://man7.org/linux/man-pages/man2/listen.2.html
int listen(int sockfd, int backlog);
And related documentation
listen() marks the socket called sockfd as a passive socket, that is, as the socket that will be used to accept incoming connection requests using accept (2).
The sockfd argument is a file descriptor that refers to a socket of type SOCK_STREAM or SOCK_SEQPACKET .
The backlog argument specifies the maximum length to which the queue of pending connections for sockfd can increase. If the connection request arrives when the queue is full, the client may receive an error indicating ECONNREFUSED or, if the underlying protocol supports retransmission, the request may be ignored, so the subsequent ECONNREFUSED connection will be ECONNREFUSED .
One additional source defines the kernel as responsible for the backlog of work.
The second backlog argument to this function specifies the maximum number of connections that the kernel should queue for this socket.
Next, they will briefly talk about how invalid / queued connections are distributed in a pool (a useful picture is included in a linked source).
To understand the backlog argument, we must understand that for a given listening socket, the kernel supports two queues:
An incomplete connection queue that contains an entry for each SYN received from a client for which the server is waiting for the TCP three-way handshake to complete. These sockets are in the SYN_RCVD state (Figure 2.4).
A complete connection queue that contains an entry for each client that has completed a three-way TCP handshake. These sockets are in the ESTABLISHED state (Figure 2.4). These two lines are shown in the figure below:
When an entry is created in an incomplete queue, parameters from the listening socket are copied to the newly created connection. The mechanism for creating a connection is fully automatic; The server process is not involved.