How to launch a Flask application using Tornado

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

from flask import Flask app = Flask(__name__) @app.route('/') def hello(): return 'Hello World!' if __name__ == '__main__': app.run() 
+3
python flask tornado
May 31 '15 at 23:53
source share
1 answer

Flask documents used to describe how to do this, but have been deleted due to performance notes below. You do not need Tornado to service your Flask application unless all of your asynchronous code is already written in Tornado.

Tornado's WSGI docs also describe this. They also contain a big warning that this is probably less efficient than using a dedicated WSGI application server such as uWSGI, Gunicorn, or mod_wsgi.

WSGI is a synchronous interface, and the Tornados concurrency model is based on single-threaded asynchronous execution. This means that running a WSGI application with Tornados WSGIContainer less scalable than running the same application on a multi-threaded WSGI server, such as gunicorn or uwsgi . Use the WSGIContainer only when there are advantages to combining Tornado and WSGI in the same process that outweighs the reduced scalability.

For example, use Gunicorn instead:

 gunicorn -w 4 app:app 



After all this, if you really really want to use Tornado, you can use the template 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() 
+11
Jun 01 '15 at 0:10
source share



All Articles