asyncore: limiting number of simultaneous connections?
brueckd at tbye.com
brueckd at tbye.com
Thu Jul 18 14:37:32 EDT 2002
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.
>
> 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)
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.
HTH,
Dave
More information about the Python-list
mailing list