Using Flask and Tornado together?

I am a big fan of Flask - partly because it is simple and partly because it has many extensions . However, Flask is intended for use in the WSGI environment, and WSGI is not blocking, so (I think) it does not scale, but Tornado for certain types of applications.

Since each of them has a URL manager that will call the function, and both will use Python files (in Django you don't run a python file, but in a jar or tornado), does it make sense to have two separate parts for your site - one part works with non-blocking works with Tornado, and the other part is written using Flask?

If this is a good idea, how would you share cookies / sessions between Flask and Tornado? Will I run into problems because Flask will use her own system and Tornado will use her own system?

+51
python flask tornado wsgi nonblocking
Nov 15 2018-11-11T00:
source share
2 answers

I think I have a 50% solution, the cookie has not been verified yet, but now I can download the Flask application using Tornado and mix Tornado + Flask together :)

first here is the flasky.py file where the jar is located:

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

and then a cyclone.py file that 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 helps someone who wants to mix them :)

+85
Nov 23 '11 at 18:44
source share
β€” -

Based on 1 and 2 combined and shorter answer

 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() 

Pay attention to the performance warning mentioned in 2 , 3

+4
Dec 30 '15 at 2:01
source share