I have a Tornado Websocket server which has a dictionary of open connections:
class WebSocketHandler(tornado.websocket.WebSocketHandler):
def open(self, *args):
self.id = self.generate_id()
self.stream.set_nodelay(True)
# ... DO STUFF ...
clients[self.id] = {"id": self.id, "time":datetime.now(), "object": self}
self.write_message("Connection SUCCESSFUL! Thanks for connecting! Your connection ID is: %d :)" % self.id)
print datetime.now()
print "New connection. ID: %d" % self.id
print "Total number of open connections: %d" % len(clients)
def on_close(self):
print datetime.now()
print "Closing connection %d." % self.id
if self.id in clients:
del clients[self.id]
print "Number of open connections: %d" % len(clients)
I would like to do automatically close all connections which are more than one hour old. So something like this:
for client in clients.values():
if (datetime.now() - client["time"])>timedelta(minutes=60):
client["object"].close()
But I don't know where I should put this check and closing old connections.
Please refer to this answer for further clarification