[英]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. 在开发Tornado应用程序时,我经常要重新启动服务器以获取新的更改。 I hit ctrl-c to stop the server, but with Tornado, this seems to be very slow.
我点击ctrl-c来停止服务器,但是使用Tornado,这似乎很慢。 It waits for many seconds before shutting down, or doesn't shut down at all when issued a ctrl-c.
在关闭之前等待很多秒,或者在发出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. 有点奇怪的是,在点击ctrl-c之后,我向服务器发出一个新请求(例如,通过刷新指向服务器的浏览器),它会立即关闭。
Anyone know how to explain this or fix it? 有谁知道如何解释或修复它? Anyone experienced something similar?
有人经历过类似的事吗
(Note, this is on Windows.) (注意,这是在Windows上。)
In Python, signals are always handled by the main thread. 在Python中,信号总是由主线程处理。 If the IOLoop is run from the main thread, it will block it when server is idle and waiting for IO.
如果从主线程运行IOLoop,它将在服务器空闲并等待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. 因此,将使用2.0秒超时调用
select
,以防止阻塞。 (See also IOLoop Timeouts ) (另请参阅IOLoop超时 )
(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) (注意:我无法在Linux上重现你的情况,即使我手动设置
select
使用,所以我不能100%保证这会有所帮助,但听起来似乎有道理)
我不知道为什么用Ctrl+C
退出需要这么长时间,但在某些情况下我按下Ctrl+\\
(Linux终端)
add_timeout works; add_timeout工作; 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()
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.