django-tools
django-tools copied to clipboard
Can werkzeug.local be used instead of threading.local in ThreadLocal.py?
Is it because django does not need to test the following conditions?
This approach, however, has a few disadvantages. For example, besides threads, there are other types of concurrency in Python. A very popular one is greenlets. Also, whether every request gets its own thread is not guaranteed in WSGI. It could be that a request is reusing a thread from a previous request, and hence data is left over in the thread local object.
Thanks! :D
Yes: Pull requests are welcome ;)
Another alternative seems to be contextvars (Python built-in). The library django-guid uses that: https://github.com/snok/django-guid/blob/main/django_guid/middleware.py And it seems they're a safe replacement for threading.local that works with asyncio as well: https://peps.python.org/pep-0567/#examples