Python sock.listen (...)

All the examples I saw in sock.listen(5) in the python documentation suggest that the maximum number of lags should be 5 . This causes a problem for my application, since I expect a very large amount (many simultaneous connections). I set it to 200 and did not see any problems in my system, but wondered how much I can install it before it causes problems.

Somebody knows?

Edit: Here is my accept () loop.

 while True: try: self.q.put(sock.accept()) except KeyboardInterrupt: break except Exception, e: self.log("ERR %s" % e) 
+6
python multithreading concurrency sockets
source share
2 answers

Doc says it

socket.listen(backlog) Listen for connections to the outlet. the backlog argument indicates the maximum number of connections connected to the queue and should be at least 1; maximum Value depends on the system (usually 5).

Obviously, the system value is greater than 5 on your system. I do not understand why installing a larger number will be a problem. Perhaps some memory is reserved for each connection connected in turn.

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 quietly truncated to this value; the default value in this file is 128. On kernels up to 2.4.25 this limit was a hard-coded SOMAXCONN value with a value of 128.

+8
source share

You do not need to tune the listen() parameter to a greater number than 5.

The parameter determines how many not accept() -ed connections are allowed to be issued. The listen() parameter is not related to the number of simultaneously connected sockets, only to the number of simultaneous connections that were not an accept() -ed process.

If setting the parameter to listen() affects your code, this is a sign that there is too much delay between each call to accept() . Then you want to change your accept() loop so that it has less overhead.

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

+15
source share

All Articles