Inside a regular application (like on Home windows), when objects/variables are produced on the global level it's open to the whole program throughout the whole duration this program is running.

Inside a web application designed in PHP for example, all variables/objects are destroyed in the finish from the script so everything needs to be written towards the database.

a) What exactly about python running under apache/modwsgi? So how exactly does that actually work regarding the memory?

b) How can you create objects that persist between web site demands and just how would you ensure there is not threads issues in apache/modwsgi?

Go browse the following in the official mod_wsgi documentation:

http://code.google.com/p/modwsgi/wiki/ProcessesAndThreading

It describes the different modes things could be run in and provides some general recommendations about data scope and discussing.

All Python globals are produced once the module is imported. When module is re-imported exactly the same globals are utilized.

Python web servers don't do threads, but pre-forked processes. Thus there's no threads difficulties with Apache.

The lifecycle of Python processes under Apache is dependent. Apache has configurations the number of child processes are created, retain in reserve and wiped out. What this means is which you can use globals in Python approaches for caching (in-process cache), however the process may terminate after any request which means you cannot put any persistent data within the globals. However the process doesn't always have to terminate and in connection with this Python is a lot more efficient than PHP (the origin code isn't parsed for each request - but you must have the server in reload mode to see source code changes throughout the expansion).

Since globals are per-process and there might be N processes, the processes share "web server global" condition using systems like memcached.

Usually Python globals only contain

  • Setting variables set throughout the procedure initialization

  • Cached data (session/user neutral)