我正在使用以下代码优雅地关闭我的龙卷风应用程序(取自https://gist.github.com/wonderbeyond/d38cd85243befe863cdde54b84505784):
def sig_handler(servers, sig, frame):
io_loop = tornado.ioloop.IOLoop.instance()
def stop_loop(deadline):
now = time.time()
if now < deadline and (io_loop._callbacks or io_loop._timeouts):
logging.info('Waiting for next tick')
print("CALL BACKS")
print(io_loop._callbacks)
print("TIMEOUTS")
print(io_loop._timeouts)
io_loop.add_timeout(now + 1, stop_loop, deadline)
else:
io_loop.stop()
logging.info("Shutting down.")
def shutdown():
logging.info("Stopping http servers")
# servers is a list of servers to stop
for s in servers:
s.stop()
logging.info("Will shutdown in %s seconds ...",
MAX_WAIT_SEC_BEFORE_SHUTDOWN)
stop_loop(time.time() + MAX_WAIT_SEC_BEFORE_SHUTDOWN)
logging.warning("Caught signal: %s", sig)
io_loop.add_callback_from_signal(shutdown)
我将MAX_WAIT_SEC_BEFORE_SHUTDOWN设置为10秒。即使关闭了http服务器,每次关闭服务器也需要完整的10秒钟。我注意到io_loop._timeouts
列表中始终存在项目,例如:
[<tornado.ioloop._Timeout object at 0x106b90408>, <tornado.ioloop._Timeout object at 0x106b904c8>, ...]
“io_loop._timeouts”中有哪些项目?我应该期望这是一个空列表,还是我没有停止应该停止的东西?
这个关闭程序正常吗?有人能提供其他代码建议吗?