django-modeltranslation
django-modeltranslation copied to clipboard
Thread / eventlet safety of context managers?
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
Good point.