我有一个Tornado Websocket服务器,我想在30分钟不活动后超时。我使用self.close()在30分钟不活动后关闭连接。但似乎有些连接在关闭之后仍然保持打开状态。在
打开连接:class WebSocketHandler(tornado.websocket.WebSocketHandler):
def open(self, *args):
self.id = self.generate_id()
self.stream.set_nodelay(True)
# ... DO STUFF ...
self.ini_time = datetime.now()
self.message_number = 0
self.last_message_time = self.ini_time
self.write_message("Connection SUCCESSFUL! Thanks for connecting! Your connection ID is: %d :)" % self.id)
self.timeout = tornado.ioloop.IOLoop.current().add_timeout(timedelta(minutes=30), self.force_close)
print datetime.now()
print "New connection. ID: %d" % self.id
print "Total number of open connections: %d" % len(clients)
收到信息时:
^{pr2}$
交割时:def on_close(self):
self.common_close()
def common_close(self):
print datetime.now()
print "Open connections are:"
print clients.keys()
print "Closing connection %d." % self.id
end = datetime.now()
timeonline = end - self.ini_time
timeconlastmsg = self.last_message_time - self.ini_time
print "Total time online:"
print timeonline
print "Time between connection start and last message received:"
print timeconlastmsg
if self.id in clients.keys():
del clients[self.id]
print "Number of open connections: %d" % len(clients)
print "Open connections are:"
print clients.keys()
超时:def force_close(self):
timout = datetime.now()
print timout
print "Connection %d timed out, server is dropping the connection." % self.id
self.common_close()
self.close()
超时工作,并调用函数force_close。但似乎即使在被调用并从clients中删除连接之后,该连接仍然是打开和活动的。在
以下是该计划的输出示例:New connection. ID: 66919
Total number of open connections: 3
2015-07-14 21:51:48.387892
New connection. ID: 12012
Total number of open connections: 4
2015-07-14 21:51:48.641603
Open connections are:
[66919, 12012, 11281, 97458]
Closing connection 66919.
Total time online:
0:00:00.404316
Time between connection start and last message received:
0:00:00
Number of open connections: 3
Open connections are:
[12012, 11281, 97458]
... ...
Number of open connections: 4
Open connections are:
[66246, 12012, 97458, 6069]
2015-07-14 22:21:47.906129
Connection 97458 timed out, server is dropping the connection.
2015-07-14 22:21:47.906167
Open connections are:
[66246, 12012, 97458, 6069]
Closing connection 97458.
Total time online:
0:30:00.000450
Time between connection start and last message received:
0:00:00
Number of open connections: 3
Open connections are:
[66246, 12012, 6069]
2015-07-14 22:21:48.237407
Connection 66919 timed out, server is dropping the connection.
2015-07-14 22:21:48.237444
Open connections are:
[66246, 12012, 6069]
Closing connection 66919.
Total time online:
0:30:00.000143
Time between connection start and last message received:
0:00:00
Number of open connections: 3
可以看出,66919被“关闭”两次,间隔30分钟。有什么想法吗?在
另一个连接3358在假定没有更多打开的连接时关闭(再次关闭两次,间隔30分钟):Open connections are:
[7046, 16287]
2015-07-15 11:01:13.604125
New connection. ID: 3358
Total number of open connections: 3
2015-07-15 11:01:28.429574
Open connections are:
[7046, 3358, 16287]
Closing connection 3358.
Total time online:
0:00:14.825568
Time between connection start and last message received:
0:00:00
Number of open connections: 2
Open connections are:
[7046, 16287]
--
Open connections are:
[]
2015-07-15 11:31:13.629530
Connection 3358 timed out, server is dropping the connection.
2015-07-15 11:31:13.629586
Open connections are:
[]
Closing connection 3358.
Total time online:
0:30:00.025556
Time between connection start and last message received:
0:00:00
Number of open connections: 0
Open connections are:
[]
有人指出我不应该在force_close中调用common_close,因为on_close将被{}调用,但是{}不会被{}调用。在