threadlocal doesn't work inside Response's app_iter #211

Closed
sayap opened this Issue Jun 8, 2011 · 1 comment

Projects

None yet

2 participants

@sayap
sayap commented Jun 8, 2011

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? This behavior seems to contradict the documentation: http://stackoverflow.com/questions/6275831/pyramid-threadlocal-doesnt-work-inside-responses-app-iter/6276143#6276143

@mcdonc
Member
mcdonc commented Jun 8, 2011

An app_iter that is a generator isn't invoked until after the threadlocal stack is popped, because the app_iter isn't iterated over until the server (or middleware that unwinds it) gets a hold of it. You'll have to obtain it outside of the app_iter and pass it in somehow.

@mcdonc mcdonc closed this Jun 8, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment