对于那些喜欢该debug=True
选项并使用Tornado-multiprocess的人,将引发以下错误:
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()
这是因为无法debug=True
加载IOLoop
并且无法加载两次。
根据文档
设置debug = True等效于autoreload = True,compiled_template_cache = False,static_hash_cache = False,serve_traceback = True。
因此,在使用debug=True
Tornado时,还会(为了方便起见)设置另一个标志:autoreload=True
“监视”更改并重新加载服务器。
该autoreload
选项是使用多进程时无法打开的选项。因此,您需要像这样配置它:
if __name__ == "__main__":
application = tornado.web.Application([
(r"/", MainHandler)
], debug=True, autoreload=False)
application.listen(8888)
tornado.ioloop.IOLoop.instance().start()