Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 asynchronous 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?

like image 238
user242486 Avatar asked May 17 '10 11:05

user242486


1 Answers

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.

like image 188
Jean-Paul Calderone Avatar answered Oct 17 '22 06:10

Jean-Paul Calderone