一尘不染

如何使用Flask-Script和Gunicorn

flask

我正在使用Flask的内置开发服务器来开发Flask应用程序。我使用Flask-Script启动它。我想切换为使用Gunicorn作为Web服务器。为此,我需要在Flask-Script和Gunicorn之间编写某种集成代码吗?还是Flask-Script与使用Gunicorn运行应用程序无关?

提前致谢!

到@ sean-lynch的道具。以下是根据他的回答而工作,经过测试的代码。我所做的更改是:

  • 在尝试启动服务器之前,将从sys.argv中删除了Gunicorn无法识别的选项remove_non_gunicorn_command_line_args()。否则,Gunicorn会引发错误,并显示以下消息:error: unrecognized arguments: --port 5010。我删除-p这是因为,即使它不会导致错误,也仅是因为Gunicorn认为它是其pidfile选择的简称,这显然不是我们想要的。

  • 修改GunicornServer.handle()签名以匹配其覆盖的方法,即Command.handle()

from flask_script import Command
from gunicorn.app.base import Application

class GunicornServer(Command):

    description = 'Run the app within Gunicorn'

    def __init__(self, host='127.0.0.1', port=8000, workers=6):

        self.port = port
        self.host = host
        self.workers = workers

    def get_options(self):
        return (
            Option('-t', '--host',
                   dest='host',
                   default=self.host),

            Option('-p', '--port',
                   dest='port',
                   type=int,
                   default=self.port),

            Option('-w', '--workers',
                   dest='workers',
                   type=int,
                   default=self.workers),
        )

    def handle(self, app, *args, **kwargs):

        host = kwargs['host']
        port = kwargs['port']
        workers = kwargs['workers']

        def remove_non_gunicorn_command_line_args():
            import sys
            args_to_remove = ['--port','-p']
            def args_filter(name_or_value):
                keep = not args_to_remove.count(name_or_value)
                if keep:
                    previous = sys.argv[sys.argv.index(name_or_value) - 1]
                    keep = not args_to_remove.count(previous)
                return keep
            sys.argv = filter(args_filter, sys.argv)

        remove_non_gunicorn_command_line_args()

        from gunicorn import version_info
        if version_info < (0, 9, 0):
            from gunicorn.arbiter import Arbiter
            from gunicorn.config import Config
            arbiter = Arbiter(Config({'bind': "%s:%d" % (host, int(port)),'workers': workers}), app)
            arbiter.run()
        else:
            class FlaskApplication(Application):
                def init(self, parser, opts, args):
                    return {
                        'bind': '{0}:{1}'.format(host, port),
                        'workers': workers
                    }

                def load(self):
                    return app

            FlaskApplication().run()

manager.add_command('gunicorn', GunicornServer())

阅读 757

收藏
2020-04-05

共1个答案

一尘不染

正如Dhaivat所说,你可以直接在Gunicorn中使用Flask应用程序。

如果你仍然想使用Flask-Script,则需要创建一个custom Command。我没有使用Gunicorn的经验,但是我发现了Flask-Actions 的类似解决方案,并将其移植到Flask-Script中,尽管会警告,但未经测试。

from flask_script import Command, Option

class GunicornServer(Command):

    description = 'Run the app within Gunicorn'

    def __init__(self, host='127.0.0.1', port=8000, workers=4):
        self.port = port
        self.host = host
        self.workers = workers

    def get_options(self):
        return (
            Option('-H', '--host',
                   dest='host',
                   default=self.host),

            Option('-p', '--port',
                   dest='port',
                   type=int,
                   default=self.port),

            Option('-w', '--workers',
                   dest='workers',
                   type=int,
                   default=self.workers),
        )

    def handle(self, app, host, port, workers):

        from gunicorn import version_info

        if version_info < (0, 9, 0):
            from gunicorn.arbiter import Arbiter
            from gunicorn.config import Config
            arbiter = Arbiter(Config({'bind': "%s:%d" % (host, int(port)),'workers': workers}), app)
            arbiter.run()
        else:
            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

            FlaskApplication().run()

然后,你可以注册它来代替瓶的本地开发服务器在python manage.py runserver

manager.add_command("runserver", GunicornServer())

或注册为新命令,例如 python manage.py gunicorn

manager.add_command("gunicorn", GunicornServer())

随着瓶脚本的最新版本,改变方法handle__call__

2020-04-05