Adding idle timeout capabilities to asyncore

Giampaolo Rodola' gnewsg at gmail.com
Mon Oct 22 15:28:50 EDT 2007


Hi there.
We're talking about an asyncore-based server.
Just for the heck of it I'd like to set a timeout which will
disconnects the clients if they're inactive (i.e., no command or data
transfer in progress) for a long period of time.
I was thinking about putting the timeout value into an attribute:

def settimeout(self, secs):
     self.timeout = time.time() + secs

And then have the readable method call time.time() at every loop to
check if time has passed or not:

def readable(self):
    if time.time() >= self.timeout:
        self.send("Timeout")
        self.close()
    return 1

My only concern is if it's a good idea calling time.time() so often.
Since A LOT of clients could be connected simultaneously, couldn't it
be a too much resource-intensive operation?
I'd also be curious to know how Twisted implemented this kind of
stuff.
By calling time.time() at every loop?


Thanks in advance.




More information about the Python-list mailing list