0

After taking a look at Django's source code, I see that Django stores the active language in the current thread.

In django.utils.translation.trans_real:

_active = local()
...
def activate(language):
    """
    Fetches the translation object for a given tuple of application name and
    language and installs it as the current translation object for the current
    thread.
    """
    _active.value = translation(language)    

This is all fine, but I'm not sure whether or not it's greenlet-safe ? I'm running Django with gunicorn, configured to run "green" gevent workers. Is local() monkey-patched by gevent? Or is there a race-condition where a request might be served using another request's active language when using gevent?

Thanks.

MiniQuark
  • 46,633
  • 36
  • 147
  • 183

1 Answers1

2

Ok nevermind, I found the answer in gevent's documentation: thread-local storage is monkey-patched by gevent and becomes greenlet-local storage. So everything should be safe.

Here's the details:

  • gevent's patch_thread() function patches the thread and threading modules, including the patch to make thread-local storage become greenlet-local storage.
  • gevent's patch_all() function calls patch_thread().
  • gunicorn calls gevent's patch_all() function when starting a gevent worker.
MiniQuark
  • 46,633
  • 36
  • 147
  • 183