简体   繁体   中英

Why does Tornado take so long to die when I hit ctrl-c?

When developing a Tornado application, I frequently want to restart the server to pick up new changes. I hit ctrl-c to stop the server, but with Tornado, this seems to be very slow. It waits for many seconds before shutting down, or doesn't shut down at all when issued a ctrl-c.

What's weird, is if, after clicking ctrl-c, I make a new request to the server (by, for example, refreshing my browser that is pointing at the server), it shuts down right away.

Anyone know how to explain this or fix it? Anyone experienced something similar?

(Note, this is on Windows.)

In Python, signals are always handled by the main thread. If the IOLoop is run from the main thread, it will block it when server is idle and waiting for IO. As a result, all signals will be pending on the thread to wake up. That explains why sending a request shuts the server down.

UPDATE: You can try something like this:

def set_ping(ioloop, timeout):
    ioloop.add_timeout(timeout, lambda: set_ping(ioloop, timeout))

and then:

ioloop = tornado.ioloop.IOLoop.instance()
set_ping(ioloop, timedelta(seconds=2))
ioloop.start()

when you start the loop. As a result, select will be called with 2.0 seconds timeout, preventing the blocking. (See also IOLoop Timeouts )

(Note: I could not reproduce your situation on Linux, even though I manually set select to be used, so I cannot give 100% guarantee that this will help, but sounds plausible)

我不知道为什么用Ctrl+C退出需要这么长时间,但在某些情况下我按下Ctrl+\\ (Linux终端)

add_timeout works; if you want to save on re-registering the timeout, instead you can do this:

ili=tornado.ioloop.IOLoop.instance()
tornado.ioloop.PeriodicCallback(lambda: None,500,ili).start()
ili.start()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM