How to run Flask app with Tornado

2020-01-27 06:12发布

问题:

I would like to run a simple app written in Flask with Tornado. How do I do this? I want to use Python 2.7 and the latest Tornado version (4.2).

from flask import Flask

app = Flask(__name__)

@app.route('/')
def hello():
    return 'Hello World!'

if __name__ == '__main__':
    app.run()

回答1:

The Flask docs used to describe how to do this, but it has been removed due to the performance notes below. You don't need Tornado to serve your Flask app, unless all your async code is already written in Tornado.

The Tornado docs about WSGI describe this as well. They also include a big warning that this is probably less performant than using a dedicated WSGI app server such as uWSGI, Gunicorn, or mod_wsgi.

WSGI is a synchronous interface, while Tornado’s concurrency model is based on single-threaded asynchronous execution. This means that running a WSGI app with Tornado’s WSGIContainer is less scalable than running the same app in a multi-threaded WSGI server like gunicorn or uwsgi. Use WSGIContainer only when there are benefits to combining Tornado and WSGI in the same process that outweigh the reduced scalability.

For example, use Gunicorn instead:

gunicorn -w 4 app:app

After all that, if you really, really still want to use Tornado, you can use the pattern described in the docs:

from tornado.wsgi import WSGIContainer
from tornado.httpserver import HTTPServer
from tornado.ioloop import IOLoop
from yourapplication import app

http_server = HTTPServer(WSGIContainer(app))
http_server.listen(5000)
IOLoop.instance().start()