Search code examples
pythonpyramid

Get handle to WSGI app in Pyramid


I need to close pserve/waitress process after forking, but I have trouble getting a reference to it so I could close it. Typically, in Pyramid __init__.py I do:

def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    ...
    config = Configurator(settings=settings,
                          authentication_policy=authn_policy,
                          authorization_policy=authz_policy,
                          root_factory=Root)
    config.scan()   
    return config.make_wsgi_app()

However, I can't store a variable with WSGI app somewhere because it's not visible in globals() in the app views, etc.

How I can get a handle to it or at least close it another way?


Solution

  • Configurator() call seems to prevent adding keys to settings dictionary in main() function, so I used a trick with adding a dictionary under settings['post_configurator_settings'] that can be used later, all of this in __init__.py of course:

    def main(global_config, **settings):
    ...
    settings['post_configurator_settings'] = {}
    config = Configurator(settings=settings,
                         ...
                          root_factory=Root)
    
    ...
    
    wsgi_app = config.make_wsgi_app()
    settings['post_configurator_settings']['wsgi_app'] = wsgi_app
    return wsgi_app