0

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.

Nick Bastin
  • 30,415
  • 7
  • 59
  • 78
sayap
  • 6,169
  • 2
  • 36
  • 40

2 Answers2

0

Maybe mistake?

return Response(app_iter=test_iter())

or

return Response(app_iter=test_iter)
estin
  • 3,051
  • 1
  • 24
  • 31
  • `app_iter` expects an iterator. Passing in just `test_iter` will cause error `TypeError: 'function' object is not iterable`. – sayap Jun 08 '11 at 08:32
0

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.

Bobby Powers
  • 2,863
  • 23
  • 15