7

I have a django instance hosted via apache/mod_wsgi. I use pre_save and post_save signals to store the values before and after save for later comparisons. For that I use global variables to store the pre_save values which can be accessed in the post_save signal handler.

My question is, if two requests A and B come together simultaneously requesting a same web service, will it be concurrent? The B should not read the global variable which is written by A and vice versa.

PS: I don't use any threading Lock on variables.

Babu
  • 2,548
  • 3
  • 30
  • 47

1 Answers1

2

This partly depends on your mod_wsgi configuration. If you configure it to use only one thread per process, then global variables are safe--although I wouldn't recommend using them, for a variety of reasons. In a multi-thread configuration, there is nothing guaranteeing that requests won't get mixed up if you use global variables.

You should be able to find some more local place to stash the data you need between pre_save and post_save. I'd recommend putting some more thought into your design.

Jamey Sharp
  • 8,363
  • 2
  • 29
  • 42
  • Seems like I need to read on `mod_wsgi` documentation :) Thanks for the tip! – K Z Nov 01 '12 at 06:33
  • 1
    @KayZhu, I'm happy to help! Check out the [WSGIDaemonProcess](http://code.google.com/p/modwsgi/wiki/ConfigurationDirectives#WSGIDaemonProcess) directive and especially the "processes" and "threads" options for some of the variations available. – Jamey Sharp Nov 01 '12 at 06:38
  • 3
    Thanks! I was just reading the same wiki at this page: [Building a portable application](http://code.google.com/p/modwsgi/wiki/ProcessesAndThreading#Building_A_Portable_Application) – K Z Nov 01 '12 at 06:41
  • 1
    @KayZhu Ah, an even better reference directly answering the question. +1! – Jamey Sharp Nov 01 '12 at 06:45
  • 3
    You can use the Python standard `threading` library to help out with thread local storage. See the [threading.local](http://docs.python.org/2/library/threading.html#threading.local) documentation. – Austin Phillips Nov 01 '12 at 06:49
  • Thread-local storage is certainly a legitimate option for request-scoped data. In this particular case, though, I wonder if the values should just be stored on the model instance that was passed to the `pre_save` and `post_save` handlers? – Jamey Sharp Nov 01 '12 at 06:52
  • so finally, do I need to combine both? `threading.local` and `WSGIDaemonProcess` with threads? – Babu Nov 01 '12 at 06:58
  • 2
    I'd advise writing your code to work regardless of how the `WSGIDaemonProcess` is configured, which means avoiding global mutable state. (Again, see [Building A Portable Application](http://code.google.com/p/modwsgi/wiki/ProcessesAndThreading#Building_A_Portable_Application) as suggested by @KayZhu.) You could use `threading.local` to produce a correct solution to your problem, but my preference is to avoid thread-local storage because it makes a program's data-flow less clear. – Jamey Sharp Nov 01 '12 at 07:02