金字塔 - threadlocal 在 Response 的 app_iter 内不起作用
使用以下示例代码:
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')
我得到当前请求:无。那么,threadlocal
在 app_iter
中不起作用?我有实际的代码,需要访问远离视图几层的 threadlocal,并且传递 request 变量会很麻烦。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据 Pyramid 文档,线程本地堆栈不应该直到使用 app_iter 后才弹出(请参阅步骤 16 和 18),尽管当我尝试运行您的示例时,我看到了与您相同的行为。由于文档和行为冲突,其中之一是错误的,我建议与 Pyramid 人员提交错误 。
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.
也许是错误?
或者
Maybe mistake?
or