threading.local equivalent for twisted.web?
In asynchronous environments, threading.local
is not guaranteed to be context-local anymore, because several contexts may coexist within a single thread. Most asyn开发者_StackOverflow社区chronous frameworks (gevent, eventlet) provide a get_current_context()
functionality to identify the current context. Some offer a way to monkey-patch threading.local
so it is local to 'greenthreads' or other framework-specific contexts. I cannot find such a functionality in the twisted documentation. How do I do this?
I'm assuming you want this API in order to save and retrieve per-request state. If not, then you might want to clarify your question.
Twisted Web doesn't offer any API along these lines. Since you're in control for the completely lifetime of the request, it's possible for you to store any per-request state yourself: on Resource
instances, in locals, in arguments to callbacks, etc. A get_current_context
function is sort of the multi-threaded equivalent of using globals to keep track of your state. When you think about it that way, hopefully it's a little more obvious why you might want to consider alternate solutions.
精彩评论