Gunicorn: 如何将 arg 传递给 gunicorn 运行的应用程序?

创建于 2010-12-09  ·  4评论  ·  资料来源: benoitc/gunicorn

现在,我像这样启动我的网络应用程序:

$ gunicorn webapp:application

我想向 webapp.py 中的应用程序函数提供一些参数。 特别是,我想使用配置文件来调整我的 web 应用程序的工作方式。

我阅读了有关如何使用配置文件配置 gunicorn 的文档,但我不想要那样。

我试过这个并得到一个错误:

$ gunicorn webapp:application dev.cfg

据我所知,gunicorn 试图找到一个名为“application dev.cfg”的可调用对象。

有任何想法吗?

最有用的评论

在您的应用程序模块中,您可以执行以下操作:

def load_app(cfg_file):
    cfg = load_app_config(cfg_file)
    return my_app(cfg)

然后你像这样启动你的应用程序:

$ gunicorn 'webapp:load_app("/path/to/my_config.ini")'

所有4条评论

在您的应用程序模块中,您可以执行以下操作:

def load_app(cfg_file):
    cfg = load_app_config(cfg_file)
    return my_app(cfg)

然后你像这样启动你的应用程序:

$ gunicorn 'webapp:load_app("/path/to/my_config.ini")'

回答。

@benoitc已经好几年了,你能告诉我这个答案是否仍然正确吗? 我一直无法让这种方法为我工作。

@pellunutty我认为现在的答案不正确。 您可以子类化应用程序类之一(例如BaseApplication )并在load_config方法中解析相关信息。 有关示例,请参见http://docs.gunicorn.org/en/stable/custom.html

此页面是否有帮助?
0 / 5 - 0 等级