启动 Pyramid 应用程序时使用 paste "call" 方案

Using the paste "call" scheme when starting a Pyramid application

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

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

我实现了一些 get_conf 方法并尝试使用 pserve call:my.module:get_conf 启动我的应用程序。如果 module/method 不存在,我会收到相应的错误,因此似乎使用了该方法。但是无论我 return 从方法中得到什么,我都会得到这个错误消息:

AssertionError: Protocol None unknown

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

虽然不是您确切问题的答案,但我认为这是您想要的答案。当金字塔启动时,ini 文件中的 ini 文件变量会被解析为在注册表中设置的设置对象,然后您可以通过注册表从应用程序的其余部分访问它们。因此,如果您想在其他地方获取设置(例如 env vars 或其他第三方来源),您需要做的就是自己构建一个工厂组件来获取它们,并在通常位于的服务器启动方法中使用它你的基础 _ _ 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