tornado 多进程模式

https://www.douban.com/note/217901726/
app

官方文档的helloworld实例中的启动方法:tornado


if __name__ == "__main__":

    application.listen(8888) # listen is a shortcut for bind and start ,这点看httpserver.py能够得知

    tornado.ioloop.IOLoop.instance().start()

而且在listen中,将tornado启动为单进程模型。

因此要启动为多进程模型的方法为:摒弃listen方法

 

    http_server = tornado.httpserver.HTTPServer(application)

    http_server.bind(options.port, options.host)

    http_server.start(num_processes=0) # tornado将按照cpu核数来fork进程

    tornado.ioloop.IOLoop.instance().start()

须要注意的一点是,要关掉debug模式,不然:
[E 110521 11:26:53 httpserver:229] Cannot run in multiple processes: IOLoop instance has already been initialized. You cannot call IOLoop.instance() before calling start()
缘由是,autoreload.py已经在http_server.start()以前就初始化了IOLoop,这个在httpserver.py中的class HTTPServer()和def start()的doc string中已经解释了。oop

相关文章
相关标签/搜索