[mod_python] example of the PythonImport directive

Graham Dumpleton grahamd at dscpl.com.au
Wed Dec 27 04:30:34 EST 2006


On 27/12/2006, at 8:20 PM, m.banaouas wrote:

> The fixup handler is probably the more elegant way to handle database
> connexion
> I suppose that this connexion (or pool of such objects) will "survive"
> (kept alive) between successive requests?

Provided it is stored in a global module variable somewhere and suitable
protection is implemented so as deal with module reloading if  
appropriate.

> you suggest to cache them in the request object: do you mean like its
> usually done with session id, added to all successif url or in hidden
> field? or should I add their reference  in the request object during
> fixup handler execution time?

The reference is added to the request object merely as a convenience so
the actual response handler doesn't itself have to go work out where to
get the database connection handle from. Thus:

   def fixuphandler(req):
     # check if database connection initialised and if not initialise it

     # cache a handle to database connection object in request object
     # so response handler can use it
     req.database = ...

     return apache.OK

Then later:

   def handler(req):
     database = req.database

     # do stuff with database

     return apache.OK

In other words, it localises a lot of the database imports potentially
to one place and you don't end up having it spread through all your
handlers as well.

> from PythonHandler family, PythonInitHandler seems to be an other  
> one appropriate for this job, is it ?

PythonInitHandler is broken and doesn't work as advertised. See:

   http://issues.apache.org/jira/browse/MODPYTHON-209

> To avoid to me any mis-understanding, the long-live connexion  
> object will reside as a global module variable ? may be that why  
> "interpretter" is usually montionned in PythonImport" threads: the  
> globals will survive because the python process is the same one and  
> is never stoped by apache?

Interpreter name has to be specified with PythonImport as at point that
that directive is processed is outside of any request so not possible to
automatically determine what interpreter instance to perform the import
for. Obviously you have to ensure it is the same as what any request
handlers use. By default it would need to be virtual host name, but  
could be
something else if using PythonInterpreter directive.

I might suggest though that you read:

   http://www.dscpl.com.au/wiki/ModPython/Articles/ 
TheProcessInterpreterModel

as your terminology suggest you might not quite understand the
relationship between process and interpreter instances.

Graham

> Graham Dumpleton a écrit :
>> To avoid having to do it in lots of different places, for example  
>> if using
>> mod_python.publisher, you can define a fixup handler which does it
>> for you. The one fixup handler will be invoked for every request  
>> in that
>> context. You could even cache a reference to the database connection
>> handle in the request object ready for the subsequent response  
>> handler
>> to use it.
>>
>> Graham
>>
>> On 27/12/2006, at 7:26 PM, m.banaouas wrote:
>>
>
>
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python



More information about the Mod_python mailing list