not thread-safe function can break a django application?

56 views
Skip to first unread message

Fabio C. Barrionuevo da Luz

unread,
Nov 22, 2014, 4:46:56 AM11/22/14
to django...@googlegroups.com
I was trying to answer a question in a Brazilian forum about django.

one of the proposed solutions utilized the function locale.setlocale() from locale module.

according to the documentation[1] locale.setlocale() function is not thread-safe

the question is: 

not thread-safe function can break a django application?
If so, how?
Which scenario I would have problems?
some easily reproducible example of problems using not thread-safe function?


[1] https://docs.python.org/2/library/locale.html#locale.setlocale

-------------------------------------- 
Fábio C. Barrionuevo da Luz
Acadêmico de Sistemas de Informação na Faculdade Católica do Tocantins - FACTO
Palmas - Tocantins - Brasil - América do Sul


Blog colaborativo sobre Python e tecnologias Relacionadas, mantido totalmente no https://github.com/pythonclub/pythonclub.github.io .

Todos são livres para publicar. É só fazer fork, escrever sua postagem e mandar o pull-request. Leia mais sobre como publicar em README.md e contributing.md.
Regra básica de postagem:
"Você" acha interessante? É útil para "você"? Pode ser utilizado com Python ou é útil para quem usa Python? Está esperando o que? Publica logo, que estou louco para ler...

Collin Anderson

unread,
Nov 24, 2014, 6:01:55 PM11/24/14
to django...@googlegroups.com


On Saturday, November 22, 2014 4:46:56 AM UTC-5, Fabio Caritas Barrionuevo da Luz wrote:
I was trying to answer a question in a Brazilian forum about django.

one of the proposed solutions utilized the function locale.setlocale() from locale module.

according to the documentation[1] locale.setlocale() function is not thread-safe

the question is: 

not thread-safe function can break a django application?
 Yes

If so, how? 
You can have "race conditions" where _sometimes_ you get unpredictable results.

Which scenario I would have problems? 
One request in one thread could set locale to English and then use the locale, meanwhile, another thread could set it to Spanish and then use the locale. If they did that at the same time, the English request might get the Spanish locale, or the Spanish request might get the English locale.

some easily reproducible example of problems using not thread-safe function?
import threading
state
= {}

def english():
    state
['locale'] = 'english'
   
assert state['locale'] == 'english'

def spanish():
    state
['locale'] = 'spanish'
   
assert state['locale'] == 'spanish'

while 1:
    threading
.Thread(target=english).start()
    threading
.Thread(target=spanish).start()
 
(press ctrl+c to stop)

Also, note that it says "If the locale is not changed thereafter, using multithreading should not cause problems." So, if you only set it once and never change it, you should be fine.

Collin

Fabio Caritas Barrionuevo da Luz

unread,
Nov 27, 2014, 2:49:37 PM11/27/14
to django...@googlegroups.com
Collin, Thanks for the explanation.
Reply all
Reply to author
Forward
0 new messages