Python sock.listen(...) Python sock.listen(...) multithreading multithreading

Python sock.listen(...)


You don't need to adjust the parameter to listen() to a larger number than 5.

The parameter controls how many non-accept()-ed connections are allowed to be outstanding. The listen() parameter has no bearing on the number of concurrently connected sockets, only on the number of concurrent connections which have not been accept()-ed by the process.

If adjusting the parameter to listen() has an impact on your code, that is a symptom that too much delay occurs between each call to accept(). You would then want to change your accept() loop such that it has less overhead.

In your case, I am guessing that self.q is a python queue, in which case you may want to call self.q.put_nowait() to avoid any possibility of blocking the accept() loop at this call.


The doc say this

socket.listen(backlog) Listen for connections made to the socket. The backlog argument specifies the maximum number of queued connections and should be at least 1; the maximum value is system-dependent (usually 5).

Obviously the system value is more than 5 on your system. I don't see why setting it to a larger number would be a problem. Perhaps some memory is reserved for each queued connection.

My linux man page has this to say

If the backlog argument is greater than the value in /proc/sys/net/core/somaxconn, then it is silently truncated to that value; the default value in this file is 128. In kernels before 2.4.25, this limit was a hard coded value, SOMAXCONN, with the value 128.