Le jeudi 17 octobre 2013 01:26:20 UTC+2, Russell Keith-Magee a écrit 

> What's wrong with thread local storage? Well, try this though experiment.
>
> Everywhere that you see the word "thread local", replace it with "global 
> variable". Now re-read your argument.
>
> It doesn't matter how you gussy it up -- a thread local is a global 
> variable, with all the software engineering consequences that follow from 
> that, including increased coupling, decreased cohesion, complications for 
> testing, and so on.
>
 

 It is a safe convention, I think. Actually every variable on the call 
stack is a thread local, because every thread has its own stack. In python 
it is possible to read variables from another thread (frame inspection), 
but you just don't do that.

A thread local is a persistent global -- I know --, but if you let it 
behave like a call stack, using context managers in Python, then it's not 
that different from a real local variable. the difference is that a 'real' 
local is on the interpreter's stack, while a thread local is in a 
pure-python stack. Both appear local.
The important part is that these stacks should look identical before and 
after execution of every function call, so that you don't have side 
effects. Python's "with"-statement is amazing at handling this.

Yes, it is more implicit than passing objects around, but it is safe. I 
think that it's also the only option we have.

Personally, for the active language, I would make that a stack as well, and 
deprecate language.activate like it is now.

with language.activate('en'):
    do_something();

About relying on that we won't have unexpected context switches in the same 
thread. That would be a safe assumption. There is one exception, called 
Gevent. what Gevent does, is patching IO routines and swapping the current 
call stack for another using a C-extension. That's a dangerous practice, 
which is unsafe by design (Also why Guido van Rossem only wants to have an 
explicit 'yield' for coroutinus.) I'm not sure, but if we want to support 
gevent with thread locals, we meight need to hook into gevent and swap our 
pure-python stacks as well.

Hopefully that explains why I think that thread-locals are not that bad as 
they look.



 
 

-- 
You received this message because you are subscribed to the Google Groups 
"Django developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to django-developers+unsubscr...@googlegroups.com.
To post to this group, send email to django-developers@googlegroups.com.
Visit this group at http://groups.google.com/group/django-developers.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-developers/78f6a3dc-c84c-4998-afa9-c85ae20efa91%40googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to