Django and threading.local() quirks?

不羁的心 提交于 2019-12-24 00:48:31

问题


Recently I've started using threading.local() as way for some apis to store and access state for duration of request without having to access request object.

So lets say I have certain code:

_thread_local = threading.local()
_thread_local.theme = 'darkblues'

How long does that _thread_local.theme variable lasts? Do I have to manually unset it at the end of request in, say, custom middleware? Or it's deleted by Django automatically after it finishes processing request?


回答1:


It will last as long as threading.local() lasts, which is the lifetime of the request. Nothing special is required after that. Django doesn't do much to manage state on thread locals, so things might stick around after the request is over. Is this really a problem? You would probably end up setting the value to whatever it needs to be in the next request.



来源:https://stackoverflow.com/questions/17385225/django-and-threading-local-quirks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!