Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does django + mod_wsgi require threaded programming discipline?

We're rolling out our first django application under mod_wsgi as

`WSGIDaemonProcess our-appname processes=6 threads=15'`

and we're having a discussion about whether our Python code and the Redis and Postgres libraries it uses needs to be thread-safe or not.

From what I can tell from reading the mod_wsgi documentation, even though the apache worker is processing requests with multiple apache threads, our python code is for all intents and purposes single-threaded. I don't see any warnings on the mod_wsgi docs saying "Beware!! You now have to worry about Global Data and Thread Safety!" but there's also no explicit "Don't Worry About Threads There Aren't Any".

We're not doing anything explicitly with threads in our python code, there is no mention of them in anything we've written.

But some people here are of the opinion that since we're running with threads=15 that we are now in in the multi-threaded world.

Can anyone clarify what's actually going on here? Is our Python code now subject to multiple threads of execution through the same data where it wasn't before, or not?

like image 526
Kevin G. Avatar asked Oct 25 '12 19:10

Kevin G.


People also ask

Does Django use threads or processes?

Django's request handler is thread-safe. You can configure your WSGI server to use any number of processes (sometimes called workers) and threads per process. As you've mentioned, processes use more memory, but threads are affected by the GIL.

Are Django models thread-safe?

Note that the Django ORM is explicitly thread-safe. There are multiple references in the documentation about threaded operation.


1 Answers

Yes obviously you are running multi-threaded app and it will create problems if you don't take care with globals, Class attributes etc

If you need to keep something globally, keep it in thread local storage.

Here is a quote from modwsgi doc, Building_A_Portable_Application

3 . An application must be re-entrant, or simply put, be able to be called concurrently by multiple threads at the same time. Data which needs to exist for the life of the request, would need to be stored as stack based data, thread local data, or cached in the WSGI application environment. Global variables within the actual application module cannot be used for this purpose.

So I think you have been sufficiently warned.

like image 107
Anurag Uniyal Avatar answered Nov 14 '22 23:11

Anurag Uniyal