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:
and then:
when you start the loop. As a result,
selectwill 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
selectto be used, so I cannot give 100% guarantee that this will help, but sounds plausible)