What's the meaning the "backlog" in the socket.listen(backlog) is?

Kushal Kumaran kushal at locationd.net
Tue Feb 16 23:04:54 EST 2021


On Tue, Feb 16 2021 at 07:24:30 PM, Jach Feng <jfong at ms4.hinet.net> wrote:
> I am experimenting with multithreading-socket these days. I build a
> server to handle each client in a separate thread. All are running on
> my local PC. It works fine except the listen() method.
>
> I set listen(2) and expect to see "error" when more clients than "the
> maximum number of queued connections" trying to connect the
> server. But, no error!! Even 4 clients can run normally without
> problem.
>
> Am I misunderstanding the meaning of this argument?
>

The argument to listen specifies the maximum number of "outstanding"
connection requests.  These are connections for which the TCP handshake
has completed, but for which the application has not called accept.
With your multi-threaded application, I assume each new connection
request gets picked up immediately by an accept call, so the queue does
not generally grow.

If you want a limit on the number of concurrent clients you want to
handle, you'll have to implement that yourself.  If you're using the
threading module, threading.Semaphore will let you implement a simple
system.

-- 
regards,
kushal


More information about the Python-list mailing list