How to set Debug mode in Tornado?

How to set Debug mode in Tornado?

I googled and found a hint that I should set debug mode in Tornado so that it could reload files automatically on update. But I didn't find an exact string to insert. I tried different combinations similar to this application.settings = {"Debug": True} but it doesn't help.

答案1

Here's a tweaked example from the tornado site:

if __name__ == "__main__":
    application = tornado.web.Application([
        (r"/", MainHandler) 
    ], debug=True)
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

答案2

For those who like the debug=True option and using Tornado-multiprocess the following error will be raised:

Traceback (most recent call last):
  File "./main.py", line 54, in <module>
    server.start(0)  # forks one process per cpu
  File "/Users/me/Library/Python/2.7/lib/python/site-packages/tornado/tcpserver.py", line 221, in start
    process.fork_processes(num_processes)
  File "/Users/me/Library/Python/2.7/lib/python/site-packages/tornado/process.py", line 130, in fork_processes
    raise RuntimeError("Cannot run in multiple processes: IOLoop instance "
RuntimeError: Cannot run in multiple processes: IOLoop instance has already been initialized. You cannot call IOLoop.instance() before calling start_processes()

This is because debug=True load IOLoop and it cannot be loaded twice.

According to the documentation

Setting debug=True is equivalent to autoreload=True, compiled_template_cache=False, static_hash_cache=False, serve_traceback=True.

So when using debug=True Tornado also sets ( for convenience ) another flag: autoreload=True which "watch" for changes and reload the server.

The autoreload option is the one that cannot be turned on when using multi-process. So you need to configure it like so:

if __name__ == "__main__":
    application = tornado.web.Application([
        (r"/", MainHandler) 
    ], debug=True, autoreload=False)
    application.listen(8888)
    tornado.ioloop.IOLoop.instance().start()

相关内容