Search code examples
pythonpyramidthread-localwebob

pyramid - threadlocal doesn't work inside Response's app_iter


With the following sample code:

from webob import Response
from paste.httpserver import serve


def test_iter():
    from pyramid import threadlocal
    yield 'current request: %s' % threadlocal.get_current_request()


def hello_world(request):
    return Response(app_iter=test_iter())


if __name__ == '__main__':
    from pyramid.config import Configurator
    config = Configurator()
    config.add_view(hello_world)
    app = config.make_wsgi_app()
    serve(app, host='0.0.0.0')

I get current request: None. So, threadlocal doesn't work inside app_iter? I have actual code where I need to access threadlocal several layers away from the view, and it would be cumbersome to pass the request variable around.


Solution

  • According to the Pyramid docs the thread-local stack shouldn't be popped until after app_iter is used (see steps 16 and 18), although I see the same behavior as you when I try to run your example. Since the documentation and behavior conflict one of them is wrong, I recommend filing a bug with the Pyramid folks.