asyncore: limiting number of simultaneous connections?

Steve Holden sholden at holdenweb.com
Thu Jul 25 10:22:35 EDT 2002


<brueckd at tbye.com> wrote in message
news:mailman.1027013966.30611.python-list at python.org...
> On Wed, 17 Jul 2002, Jason R.Mastaler wrote:
>
> > I'm building a server based on asynchat/asyncore.  The
> > documentation for these modules is sketchy at best, and I couldn't
> > find an answer to this after browsing through the code.
> >
Just thought I'd mention that the development version of the docs has
updated the asyncore documentation, as well as providing asynchat docs for
the first time. I made these changes, and I'd appreciate feedback if you
have any -- there's still time to make further changes to improve clarity.

> > 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.

The problem with doing network code well is coping with the unlikely
(low-probability) events that occur all the time! ;-)

> For other reasons I used a different approach in one program: have the
> accepting socket be on another thread, and it controlled access that way
> (when it was ok it have another connection it would accept it and add it
> to asyncore with:
>
> asynchat.async_chat.__init__(self, conn)
>
> Also, you can check len(asyncore.socket_map) to see how many sockets it is
> currently handling.

Assuming you're using the default map.

regards
-----------------------------------------------------------------------
Steve Holden                                 http://www.holdenweb.com/
Python Web Programming                http://pydish.holdenweb.com/pwp/
-----------------------------------------------------------------------








More information about the Python-list mailing list