How to use Flask-Script and Gunicorn

前端 未结 7 570
[愿得一人]
[愿得一人] 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:09

    I wrote a better version of GunicornServer based on Sean Lynch's, the command now accept all gunicorn's arguments

    from yourapp import app
    from flask.ext.script import Manager, Command, Option
    
    class GunicornServer(Command):
        """Run the app within Gunicorn"""
    
        def get_options(self):
            from gunicorn.config import make_settings
    
            settings = make_settings()
            options = (
                Option(*klass.cli, action=klass.action)
                for setting, klass in settings.iteritems() if klass.cli
            )
            return options
    
        def run(self, *args, **kwargs):
            from gunicorn.app.wsgiapp import WSGIApplication
    
            app = WSGIApplication()
            app.app_uri = 'manage:app'
            return app.run()
    
    manager = Manager(app)
    manager.add_command("gunicorn", GunicornServer())
    

提交回复
热议问题