'Is storing data in "thread local storage" in a Django application safe, in cases of concurrent requests?

I have seen at many places that using thread local storage to store any data in Django application is not a good practice. But this is the only way I could store my request object. I need to store it because my application has a complex structure. And I can't keep on passing the request object at each function call or class intialization.

I need the cookies and headers from my request object, to be passed to some api calls I'm making at different places in the application.

I'm using this for reference: https://blndxp.wordpress.com/2016/03/04/django-get-current-user-anywhere-in-your-code-using-a-middleware/

So I'm using a middleware, as mentioned in the reference. And, this is how request is stored

from threading import local
_thread_locals = local()
_thread_locals.request = request

And, this is how data is fetched:

getattr(_thread_locals, "request", None)

So does are the data stored in the threads local to that particular request ? Or if another request takes place at the same time, does both of them use the same data ?(Which is certainly not what i want)

Or is there any new way of dealing with this old problem(storing request object globally)

Note: I'm also using async at places in my Django application(If that matters).



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source