I'm using the following code to close down my tornado application gracefully (taken from 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)
I set MAX_WAIT_SEC_BEFORE_SHUTDOWN to 10 seconds. Even after closing down the http servers it takes the full 10 seconds everytime to close down the server. I have noted that there are always items in the io_loop._timeouts
list E.g:
[<tornado.ioloop._Timeout object at 0x106b90408>, <tornado.ioloop._Timeout object at 0x106b904c8>, ...]
What are the items in io_loop._timeouts
? Should I expect this to be an empty list or am I not stopping something that i should have?
Is this shutdown routine normal? Can anyone suggest other code?