{"action":"created","issue":{"url":"https://api.github.com/repos/Pylons/pyramid/issues/596","repository_url":"https://api.github.com/repos/Pylons/pyramid","labels_url":"https://api.github.com/repos/Pylons/pyramid/issues/596/labels{/name}","comments_url":"https://api.github.com/repos/Pylons/pyramid/issues/596/comments","events_url":"https://api.github.com/repos/Pylons/pyramid/issues/596/events","html_url":"https://github.com/Pylons/pyramid/issues/596","id":4424781,"number":596,"title":"For consideration: an API to start / teardown a request context","user":{"login":"wichert","id":199657,"avatar_url":"https://avatars.githubusercontent.com/u/199657?v=3","gravatar_id":"","url":"https://api.github.com/users/wichert","html_url":"https://github.com/wichert","followers_url":"https://api.github.com/users/wichert/followers","following_url":"https://api.github.com/users/wichert/following{/other_user}","gists_url":"https://api.github.com/users/wichert/gists{/gist_id}","starred_url":"https://api.github.com/users/wichert/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/wichert/subscriptions","organizations_url":"https://api.github.com/users/wichert/orgs","repos_url":"https://api.github.com/users/wichert/repos","events_url":"https://api.github.com/users/wichert/events{/privacy}","received_events_url":"https://api.github.com/users/wichert/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/Pylons/pyramid/labels/enhancements","name":"enhancements","color":"02e10c"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":9,"created_at":"2012-05-04T14:31:35Z","updated_at":"2016-04-28T02:20:17Z","closed_at":"2016-04-28T02:20:17Z","body":"When writing console scripts you often need a configured pyramid, which is handled by pyramid.paster.bootstrap. Sometimes you need more than that though: you also need a fully configured request context. For example to get transaction handling or other things provided by tweens, or to have extra request properties setup. A possible API for that would be something like:\r\n\r\n```python\r\n\r\nenvironment = bootstrap(config_file)\r\nwith environment['request-context']:\r\n    # Do my thing\r\n```\r\n\r\nwhere the request context would setup the tweens, issue INewRequest, etc. on `__enter__` and do all the necessary teardown on `__exit__`.\r\n"},"comment":{"url":"https://api.github.com/repos/Pylons/pyramid/issues/comments/215289025","html_url":"https://github.com/Pylons/pyramid/issues/596#issuecomment-215289025","issue_url":"https://api.github.com/repos/Pylons/pyramid/issues/596","id":215289025,"user":{"login":"mmerickel","id":487237,"avatar_url":"https://avatars.githubusercontent.com/u/487237?v=3","gravatar_id":"","url":"https://api.github.com/users/mmerickel","html_url":"https://github.com/mmerickel","followers_url":"https://api.github.com/users/mmerickel/followers","following_url":"https://api.github.com/users/mmerickel/following{/other_user}","gists_url":"https://api.github.com/users/mmerickel/gists{/gist_id}","starred_url":"https://api.github.com/users/mmerickel/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/mmerickel/subscriptions","organizations_url":"https://api.github.com/users/mmerickel/orgs","repos_url":"https://api.github.com/users/mmerickel/repos","events_url":"https://api.github.com/users/mmerickel/events{/privacy}","received_events_url":"https://api.github.com/users/mmerickel/received_events","type":"User","site_admin":false},"created_at":"2016-04-28T02:20:17Z","updated_at":"2016-04-28T02:20:17Z","body":"I just ran across this issue and it should be mostly solved at this point. The main feature is that request methods and properties are now attached properly which replaces most uses of the `NewRequest` event.\r\n\r\nFor a long-running script it's possible now to build a registry using the building blocks of bootstrap (bootstrap itself is coupled to a single request, but the building blocks are not).\r\n\r\n```python\r\napp = pyramid.paster.get_app(inipath)\r\nregistry = app.registry\r\n\r\nfor job in jobs:\r\n    req = Request.blank(...)\r\n    env = pyramid.scripting.prepare(registry=registry, request=request)\r\n    env['closer']()\r\n```\r\n\r\nUsing this method you can load the app once and re-use it across several dummy requests. You are still responsible for defining your own pipeline however, including invoking any events you care about and managing transactions.\r\n\r\nIf you really need to simulate a request with a full router lifecycle (tweens and events) then `prequest` is a good option or you can define your logic in a pyramid view, sending your request through the router.\r\n\r\n```\r\ndef your_worker_view(request):\r\n    # do some work\r\n\r\nconfig = Configurator()\r\nconfig.include(your_app)\r\nconfig.add_view(your_worker_view, name='worker1234')\r\napp = config.make_wsgi_app()\r\n\r\nreq = Request.blank('worker1234', ...)\r\nresponse = req.get_response(app)\r\n```"}}