简体   繁体   English

在一个请求期间龙卷风存储上下文,还是获取当前的请求处理程序?

[英]Tornado store context during one request, or get current request handler?

I wonder if there's something like flask.g in Tornado, to easily share context between co-routines in one request. 我想知道在Tornado中是否有诸如flask.g之类的东西,可以在一个请求中轻松地在协同例程之间共享上下文。 Or, may I just use the request handler object as the context, but how could I get the current one? 或者,我可以仅使用请求处理程序对象作为上下文,但是如何获取当前对象呢?

Tornado does not provide an equivalent to flask.g . 龙卷风没有提供flask.g的等效flask.g Things like thread-local variables do not work as usual in an asynchronous framework since everything is running on the same thread. 诸如线程局部变量之类的事情在异步框架中无法正常工作,因为所有内容都在同一线程上运行。

It is possible to build something analogous to a thread-local variable with a StackContext , but this is discouraged because it has significant overhead and some third-party libraries do not propagate stack contexts correctly. 可以使用StackContext构建类似于线程局部变量的StackContext ,但是不建议这样StackContext ,因为它具有大量开销,并且某些第三方库无法正确传播堆栈上下文。 In most cases I would recommend explicitly passing the information you need to the places that need it instead of introducing this kind of magic. 在大多数情况下,我建议您将所需的信息明确传递给需要的信息,而不要引入这种魔术。 But if you think this is the best solution for your situation, see https://github.com/viewfinderco/viewfinder/blob/master/backend/base/context_local.py for an example. 但是,如果您认为这是适合您情况的最佳解决方案,请参见https://github.com/viewfinderco/viewfinder/blob/master/backend/base/context_local.py

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM