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?
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.
As you can see, the Tornado implementation is significantly faster than Flask.
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.
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.
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 :)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With