[mod_python] Quick question from mod_perl user

Graham Dumpleton grahamd at dscpl.com.au
Sun Jan 23 00:20:11 EST 2005

On 23/01/2005, at 4:08 PM, snacktime wrote:

>> I am not familiar with mod_perl, and thus not totally clear on what  
>> you
>> mean by "persist" in this particular case, but a global variable  
>> within
>> a module will in mod_python persist across distinct requests.
>> This global though is only accessible to that specific Python
>> interpreter,
>> thus if using prefork mode of Apache, different processes obviously  
>> have
>> different versions of that global. If in one process, distinct
>> interpreters
>> are created, again each interpreter, each has their own version of  
>> that
>> global.
>> Obviously, a global does not persist across restarts.
> Ok, so far this is the same as mod perl.
> With mod perl I normally create my own request object, usually a
> hash/dictionary to store all the values I need to be global.  At the
> end of the request I just undefine the one request object instead of
> having to remember all the different global variables.  Is this
> approach good for mod python as well?

In mod_python, to communicate between handlers or even methods  
a request, one can add bits into the existing request object. The  
even says:


   You can dynamically assign attributes to it as a way to communicate  
between handlers.


     A table object that could be used to store miscellaneous  general  
     info that lives for as long as the request lives. If  you need to  
pass data
     between handlers, it's better to simply add  members to the request  
     than to use notes.


   def accesshandler(req):
     req.mystuff = { "a": 1 }
     return apache.OK

   def handler(req):
     if req.mystuff["a"] == ...:
     return apache.OK

You don't even have to undefine your stuff as the "req" object which  
holds it
will be destroyed at the end of the request anyway.

If you need to do more complicated stuff whereby you need to execute  
special code to cleanup at the end of a request, you can also look at  


   register_cleanup(callable[, data])

     Registers a cleanup. Argument callable can be any callable  object,
     the optional argument data can be any object (default is None). At  
the very
     end of the request, just before the actual  request record is  
     by Apache, callable will be called  with one argument, data.

     It is OK to pass the request object as data, but keep in mind that
     when the cleanup is executed, the request processing is already  
     so doing things like writing to the client is completely  pointless.

     If errors are encountered during cleanup processing, they should be
     in  error log, but otherwise will not affect request processing in  
     way, which makes cleanup bugs sometimes hard to spot.

     If the server is shut down before the cleanup had a chance to run,
     it's possible that it will not be executed.

Hope this helps.


More information about the Mod_python mailing list