using Flask and Tornado together?

前端 未结 2 467
北恋
北恋 2020-12-02 03:47

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 environmen

相关标签:
2条回答
  • 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

    0 讨论(0)
  • 2020-12-02 04:46

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

    0 讨论(0)
提交回复
热议问题