Pylons / pyramid

Pyramid - A Python web framework
https://trypyramid.com/
Other
3.95k stars 882 forks source link

Adding script info to global_config during application setup #3734

Closed adroullier closed 8 months ago

adroullier commented 11 months ago

Pyramid startup scripts in python/bin directory (pserve, prequest, pshell) do not pass any special info to the wsgi app or "main()" function. As far as I know there is no way to find out how the wsgi application startup is triggered.

In some cases it makes sense to call different code whether the application is created as server (bin/pserve) or for a single request (bin/prequest). For example setting up internal caching modules, maintenance or other background functions.

Right now you have use extra environment variables, multiple wsgi configurations or other workarounds which are not really elegant solutions.

My proposal is to extend the scripts with additional context information which simply refer to the scripts name. The additional info is passed to the main function as part of global_config["__script__"] dict during wsgi setup.

For example, from a developers perspective it would look like:

from pyramid.config import Configurator

def main(global_config, **settings):
    config = Configurator()
    config.include('myapp.myconfig.includeme')

    # global_config.get("__script__") = either "prequest", "pserve", "pshell" or None 
    # signals how the application startup was triggered
    # if required handle application setup accordingly

I can provide a pull request if there a no objections. And the code change should have no impact on existings applications.

mmerickel commented 11 months ago

I don't currently have a good vision on how this would be implemented - if it can be done in pyramid's shell scripts directly, and/or if plaster/plaster-pastedeploy would need to assist in some way but I'm happy to consider a PR to add this idea of marshaling a value through into the global_config.

mmerickel commented 8 months ago

Fixed via https://github.com/Pylons/pyramid/pull/3735.