The global state problem is something that's been bothering me for a long while, but after seeing a presentation of Alex Gaynor [1] from last year, I started thinking about this again.
The main problem is that you'd need to have a DjangoProject object which contains the root configuration and you'd have to somehow pass that object around everywhere.Maybe I'm not the first to think about this, but what's wrong with thread-local-storage?
In Django, we have a huge advantage above nodejs in that there's only one active request per thread and that request should belong to only one DjangoProject instance. (We don't have unexpected contextswitches in the same thread, like in Twisted or Tulip. -- We can make that assumption.)
Actually we are already using threadlocals for the current active language [2], so I don't see any reason for not using the same approach to keep track of the currently active DjangoApplication object.
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.
On Oct 17, 2013 7:13 AM, "Jonathan Slenders" <jonathan...@gmail.com> wrote:
> 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.
Thread locals are "greenlet locals" in gevent so this isn't an issue.
But like Russell I doubt this proposal actually solves the problem.
For instance, thread locals are strictly equivalent to regular variables in tests because they are single threaded (with a handful of exceptions). But allowing testing in isolation is a major goal of "removing global state".
--
Aymeric.