jedie / django-tools

miscellaneous django tools
http://pypi.python.org/pypi/django-tools/
Other
128 stars 31 forks source link

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

Open huazhaozhe opened 5 years ago

huazhaozhe commented 5 years ago

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

jedie commented 5 years ago

Yes: Pull requests are welcome ;)

fjsj commented 1 year ago

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