使用粘贴"调用"启动Pyramid应用程序时的方案

时间:2015-11-27 09:27:16

标签: python pyramid paster

我有一个金字塔应用程序,我可以开始使用pserve some.ini。 ini文件包含通常的粘贴配置,一切正常。在制作中,我使用uwsgi,有一个paste = config:/path/to/some.ini条目,也可以正常工作。

但是我不是从静态ini文件中读取配置,而是想从一些外部键值存储中检索它。阅读paste文档和源代码,我发现有一个call方案,它调用python函数来检索"设置"。

我实施了一些get_conf方法并尝试使用pserve call:my.module:get_conf启动我的应用程序。如果模块/方法不存在,我会得到一个适当的错误,因此似乎使用了该方法。但无论我从该方法返回什么,我最终都会收到此错误消息:

  

AssertionError:协议无未知

我不知道该方法的返回值是什么以及如何实现它。我试图找到文档或示例,但没有成功。我该如何实现这种方法?

1 个答案:

答案 0 :(得分:2)

虽然不是您确切问题的答案,但我认为这是您想要的答案。当金字塔启动时,来自ini文件的ini文件变量只会被解析为注册表中设置的设置对象,并且您可以通过注册表从应用程序的其余部分访问它们。因此,如果您想在其他地方获取设置(例如env vars或其他第三方来源),您需要做的就是为自己构建一个工厂组件,并在服务器启动方法中使用它,通常在你的base _ _ init _ _.py文件。如果不合适,你不需要从ini文件中获取任何内容,如果你不这样做,那么你的部署方式并不重要。您应用的其余部分并不需要知道它们来自何处。以下是我如何通过env vars获取设置的示例,因为我有一个包含三个独立进程的分布式应用程序,而且我不想使用三组ini文件(相反,我有)一个env vars文件,它不会进入git并在开启任何内容之前获取源代码):

# the method that runs on server startup, no matter
# how you deploy. 
def main(global_config, **settings):
""" This function returns a Pyramid WSGI application."""

    # settings has your values from the ini file
    # go ahead and stick things it from any source you'd like
    settings['db_url'] = os.environ.get('DB_URL')
    config = Configurator(
        settings=settings,
    # your other configurator args
    )
    # you can also just stick things directly on the registry
    # for other components to use, as everyone has access to
    # request.registry. 
    # here we look in an env var and fall back to the ini file
    amqp_url = os.environ.get('AMQP_URL', settings['amqp.url'] )
    config.registry.bus = MessageClient( amqp_url=amqp_url )

    # rest of your server start up code.... below