django-tools icon indicating copy to clipboard operation
django-tools copied to clipboard

Can werkzeug.local be used instead of threading.local in ThreadLocal.py?

Open huazhaozhe opened this issue 5 years ago • 2 comments

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.

werkzeug.local Context Locals

Thanks! :D

huazhaozhe avatar Aug 25 '19 12:08 huazhaozhe

Yes: Pull requests are welcome ;)

jedie avatar Aug 25 '19 18:08 jedie

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

fjsj avatar Nov 18 '22 22:11 fjsj