【问题标题】:pyramid - threadlocal doesn't work inside Response's app_iter金字塔 - threadlocal 在 Response 的 app_iter 中不起作用
【发布时间】:2011-06-08 08:00:12
【问题描述】:

使用以下示例代码:

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')

我得到当前请求:无。那么,threadlocalapp_iter 中不起作用?我有实际代码,我需要访问远离视图几层的threadlocal,并且传递request 变量会很麻烦。

【问题讨论】:

    标签: python pyramid thread-local webob


    【解决方案1】:

    根据Pyramid docs,在使用 app_iter 之前不应弹出线程本地堆栈(请参阅步骤 16 和 18),尽管我在尝试运行您的示例时看到了与您相同的行为。由于文档和行为冲突,其中一个是错误的,我向 Pyramid 的人推荐 filing a bug

    【讨论】:

    • 啊,这实际上是预期的行为。
    【解决方案2】:

    可能是错误?

    return Response(app_iter=test_iter())
    

    return Response(app_iter=test_iter)
    

    【讨论】:

    • app_iter 需要一个迭代器。只传入test_iter 会导致错误TypeError: 'function' object is not iterable
    猜你喜欢
    • 2012-09-27
    • 1970-01-01
    • 1970-01-01
    • 2013-09-23
    • 2014-05-11
    • 1970-01-01
    • 2012-03-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多