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

Thread / eventlet safety of context managers?

Open rmmr opened this issue 7 years ago • 1 comments

I was looking at the inner workings of the translation fallback mechanisms, mostly looking at the helper functions defined at /utils.py . I was curious how you handled concurrency for the context manager fallbacks(). This manager function directly modifies settings.ENABLE_FALLBACKS without accounting for any concurrency?

This won't cause any problems for sync workers, but both threaded and async workers can (and most likely will) run into problems. I highly recommend the use of of a Thread Local's object. If you look at Django's source code you will find plently of examples e.g. https://github.com/django/django/blob/5a6f70b4281817656db2f36c5919036d38fcce7f/django/core/cache/init.py

rmmr avatar Nov 27 '17 07:11 rmmr

Good point.

zlorf avatar Nov 27 '17 09:11 zlorf