[Django]-What is so bad with threadlocals

18👍

I don’t think there is anything wrong with threadlocals – yes, it is a global variable, but besides that it’s a normal tool. We use it just for this purpose (storing subdomain model in the context global to the current request from middleware) and it works perfectly.

So I say, use the right tool for the job, in this case threadlocals make your app much more elegant than passing subdomain model around in all the model methods (not mentioning the fact that it is even not always possible – when you are overriding django manager methods to limit queries by subdomain, you have no way to pass anything extra to get_query_set, for example – so threadlocals is the natural and only answer).

22👍

I avoid this sort of usage of threadlocals, because it introduces an implicit non-local coupling. I frequently use models in all kinds of non-HTTP-oriented ways (local management commands, data import/export, etc). If I access some threadlocals data in models.py, now I have to find some way to ensure that it is always populated whenever I use my models, and this could get quite ugly.

In my opinion, more explicit code is cleaner and more maintainable. If a model method requires a subdomain in order to operate, that fact should be made obvious by having the method accept that subdomain as a parameter.

If I absolutely could find no way around storing request data in threadlocals, I would at least implement wrapper methods in a separate module that access threadlocals and call the model methods with the needed data. This way the models.py remains self-contained and models can be used without the threadlocals coupling.

3👍

Also a lot of Java frameworks seem to be using threadlocals a lot, so how is their case different from Python/Django ‘s?

CPython’s interpreter has a Global Interpreter Lock (GIL) which means that only one Python thread can be executed by the interpreter at any given time. It isn’t clear to me that a Python interpreter implementation would necessarily need to use more than one operating system thread to achieve this, although in practice CPython does.

Java’s main locking mechanism is via objects’ monitor locks. This is a decentralized approach that allows the use of multiple concurrent threads on multi-core and or multi-processor CPUs, but also produces much more complicated synchronization issues for the programmer to deal with.

These synchronization issues only arise with “shared-mutable state”. If the state isn’t mutable, or as in the case of a ThreadLocal it isn’t shared, then that is one less complicated problem for the Java programmer to solve.

A CPython programmer still has to deal with the possibility of race conditions, but some of the more esoteric Java problems (such as publication) are presumably solved by the interpreter.

A CPython programmer also has the option to code performance critical code in Python-callable C or C++ code where the GIL restriction does not apply. Technically a Java programmer has a similar option via JNI, but this is rightly or wrongly considered less acceptable in Java than in Python.

👤richj

2👍

You want to use threadlocals when you’re working with multiple threads and want to localize some objects to a specific thread, eg. having one database connection for each thread.
In your case, you want to use it more as a global context (if I understand you correctly), which is probably a bad idea. It will make your app a bit slower, more coupled and harder to test.

Why is passing it from request not worth it? Why don’t you store it in session or user profile?

There difference with Java is that web development there is much more stateful than in Python/PERL/PHP/Ruby world so people are used to all kind of contexts and stuff like that. I don’t think that is an advantage, but it does seem like it at the beginning.

👤Seb

0👍

I have found using ThreadLocal is an excellent way to implement Dependency Injection in a HTTP request/response environment (i.e. any webapp). You just set up a servlet filter to ‘inject’ the object you need into the thread on receiving the request and ‘uninject’ it on returning the response.

It’s a smart man’s DI without all the XML ugliness, without the MB of Spring Jars (not to mention its learning curve) and without all the cryptic repetitive @annotation nonsense and because it doesn’t individually inject many object instances with the dependencies it’s probably a heck of a lot faster and uses less memory.

It worked so well we opened sourced our exPOJO Filter that can inject a Hibernate session or a JDO PersistenceManager using ThreadLocal:

http://www.expojo.com

Leave a comment