Is middleware in Django thread-safe?

Viewed 4882

Recently I've read this article: http://blog.roseman.org.uk/2010/02/01/middleware-post-processing-django-gotcha/

I don't understand, why does the solution described there work?

Why does instantiating separate objects make data chunk thread-safe?

I have two guesses:

  • Django explicitly holds middleware objects in shared memory, and do not do this for other objects, so other objects are thread-safe.
  • In second example, in article, lifetime of thread-safety-critical data is much less that in first example, so probably, thread-unsafe operations just have no time to occur.

There is also issues with thread-safety in Django templates.

My question is - how to guess when Django thread-safe and where its not? is there any logic in it or conventions? Another question - I know that request object is thread safe - it is clear, that it wouldn't be safe, web-sites built with Django would be not able to operate, but what exactly makes it thread-safe?

1 Answers
Related