Python sock.listen(…)

前端 未结 2 1019
天命终不由人
天命终不由人 2020-12-10 06:39

All the examples I\'ve seen of sock.listen(5) in the python documentation suggest I should set the max backlog number to be 5. This is causing a pr

相关标签:
2条回答
  • 2020-12-10 06:47

    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.

    0 讨论(0)
  • 2020-12-10 07:01

    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.

    0 讨论(0)
提交回复
热议问题