Life cycle of a mod_python application and scope of global variables

Kyle Yancey lakeofburningfire at kyle.orgdon'tspamme
Fri Jan 24 13:13:49 EST 2003


I've been trying out mod_python + apache + WinXP lately.  I was
wondering if I could get some help clarifying a few things.

My primary concern is with the lifespan of global variables.  As I
understand it, in mod_python a global variable is initialized when the
handler script is first started.  Global variables are not released
when the instance of the script ceases, and the variable is
essentially static and global for all instances of the handler script.

example:

#!/usr/bin/env python
from mod_python import apache

def handler(req):
   req.content_type = "text/plain" 
   req.send_http_header()
   req.write(i)
   return apache.OK



Consecutive executions of this script will output
1
2
3
4
5
etc.

Are global variables just shared between multiple threads, and should
they be protected with mutexes to prevent race conditions.  I'm a bit
confused about this since I'm just now learning about multithreading.
I'm aware that the smart thing to do is to just not use them, but I
hate being uncertain about what is going on.




More information about the Python-list mailing list