How to use Flask-Script and Gunicorn

前端 未结 7 569
[愿得一人]
[愿得一人] 2020-12-02 12:39

I\'m working on on a Flask app using Flask\'s built in dev server. I start it using Flask-Script. I want to switch to using Gunicorn as the web server. To do so, do I nee

7条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-02 13:16

    Based the answer of the Sean, I also wrote a version more preferred to me.

    @manager.option('-h', '--host', dest='host', default='127.0.0.1')
    @manager.option('-p', '--port', dest='port', type=int, default=6969)
    @manager.option('-w', '--workers', dest='workers', type=int, default=3)
    def gunicorn(host, port, workers):
        """Start the Server with Gunicorn"""
        from gunicorn.app.base import Application
    
        class FlaskApplication(Application):
            def init(self, parser, opts, args):
                return {
                    'bind': '{0}:{1}'.format(host, port),
                    'workers': workers
                }
    
            def load(self):
                return app
    
        application = FlaskApplication()
        return application.run()
    

    you can run the gunicorn using command like thispython manager.py gunicorn

提交回复
热议问题