相关疑难解决方法(0)

如何使用Flask-Script和Gunicorn

我正在使用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 …
Run Code Online (Sandbox Code Playgroud)

python flask gunicorn

17
推荐指数
3
解决办法
1万
查看次数

标签 统计

flask ×1

gunicorn ×1

python ×1