Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using Flask and Tornado together?

I am a big fan of Flask - in part because it is simple and in part because has a lot of extensions. However, Flask is meant to be used in a WSGI environment, and WSGI is not a non-blocking, so (I believe) it doesn't scale as well as Tornado for certain kinds of applications.

Since each one has an URL dispatcher which will call a function, and both will use Python files (in Django you dont launch the python file but in flask or tornado you do) do does it make sense to have two seperate parts to your website - one part running the non-blocking jobs with Tornado, and the other part written with Flask?

If this is a good idea, how would you go about sharing cookies / sessions between Flask and Tornado? Will I run into issues, since Flask will use it own system and Tornado will use its own system?

like image 581
Abdelouahab Avatar asked Nov 15 '11 21:11

Abdelouahab


People also ask

When should you not use a Flask?

When running publicly rather than in development, you should not use the built-in development server ( flask run ). The development server is provided by Werkzeug for convenience, but is not designed to be particularly efficient, stable, or secure.

What is faster Flask or Tornado?

As you can see, the Tornado implementation is significantly faster than Flask.

Can Flask run multiple threads?

As of Flask 1.0, flask server is multi-threaded by default. Each new request is handled in a new thread. This is a simple Flask application using default settings.

Does Flask support concurrent requests?

Flask will process one request per thread at the same time. If you have 2 processes with 4 threads each, that's 8 concurrent requests. Flask doesn't spawn or manage threads or processes.


2 Answers

i think i got 50% of the solution, the cookies are not tested yet, but now i can load Flask application using Tornado, and mixing Tornado + Flask together :)

first here is flasky.py the file where the flask application is:

from flask import Flask app = Flask(__name__)  @app.route('/flask') def hello_world():   return 'This comes from Flask ^_^' 

and then the cyclone.py the file which will load the flask application and the tornado server + a simple tornado application, hope there is no module called "cyclone" ^_^

from tornado.wsgi import WSGIContainer from tornado.ioloop import IOLoop from tornado.web import FallbackHandler, RequestHandler, Application from flasky import app  class MainHandler(RequestHandler):   def get(self):     self.write("This message comes from Tornado ^_^")  tr = WSGIContainer(app)  application = Application([ (r"/tornado", MainHandler), (r".*", FallbackHandler, dict(fallback=tr)), ])  if __name__ == "__main__":   application.listen(8000)   IOLoop.instance().start() 

hope this will help someone that wants to mix them :)

like image 104
Abdelouahab Avatar answered Sep 23 '22 21:09

Abdelouahab


Based on 1 and 2, the combined and shorter answer is

from flask import Flask app = Flask(__name__)  @app.route("/") def hello():     return "Hello World!"  if __name__ == "__main__":      from tornado.wsgi import WSGIContainer     from tornado.httpserver import HTTPServer     from tornado.ioloop import IOLoop      http_server = HTTPServer(WSGIContainer(app))     http_server.listen(8000)     IOLoop.instance().start() 

Please consider the warning about performance that has been mentioned on 2 , 3

like image 31
Ahmad Yoosofan Avatar answered Sep 22 '22 21:09

Ahmad Yoosofan