asyncore: limiting number of simultaneous connections?

brueckd at tbye.com brueckd at tbye.com
Thu Jul 25 12:48:08 EDT 2002


On Thu, 25 Jul 2002, Steve Holden wrote:

> > > Is there an easy way to limit the number of simultaneous connections
> > > to such a server?  My preliminary tests indicate that it will happily
> > > accept as many connections as the host system will allow, which is not
> > > good.  I'd like to be able to specify a ceiling on this.
> >
> > You should be able to just implement your own readable method in the
> > listening socket, e.g.  (untested and assumes your listening socket is
> > derived from asnychat.async_chat):
> >
> > def readable(self):
> >   if not self.AllowedToAcceptMoreConnections(): # You implement this
> >     return 0
> >   return asynchat.async_chat.readable(self)
> >
> Technically, of course, this isn't strictly going to limit the number of
> connections to any given maximum since it only determines under what
> conditions the listening socket returns readable(), and readable() is used
> by asyncore to determine whether the socket should be added to the set for
> which a read condition is accepted. Any time the socket is so listed (in the
> select.select() arguments) any number of simultaneous connections might
> occur, though this is unlikely.

No, it's not that unlikely for many servers In any case your listening
socket will then accept one, and only one, new connection - the rest stay
on the listen queue. Maybe you could elaborate a little more on what you
mean because I've used the method I describe above and it's a perfectly
acceptable way to throttle your server.

I guess if you want to get nitpicky then the above method limits the 
number of simultaneous connections to N + M, where N is the number your 
limiting function uses and M is the size of your listen queue, but it's 
still a fixed maximum. ;-)

-Dave





More information about the Python-list mailing list