On 13/11/2006, at 8:01 PM, g.farina at pharaondev.com wrote:
> Hi
>> I just started to develop with modpython and I have some questions
> about
> quite simple topics.
Not necessarily simple, as for some they open a can of worms. :-)
> First of all, correct me if I wrong, I read that modpython works as a
> layer between Apache APIs and Python scripts: when apache needs an
> handler
> for a particular operation, it asks modpython for this handler and
> modpython returns it based on some configurations.
> Is that right?
Not exactly. Apache doesn't strictly ask mod_python for a handler.
For certain Apache request handling phases, the Python*Handler
directives are used to register mod_python handlers. The registration
causes Apache to actually pass handling of the request through to
mod_python at that point and mod_python invokes the handler.
The handler can simply return a status value, or it could write actual
content back for the response via the request object.
In the case of the actual response handler phase, it is also necessary
to tell Apache that the mod_python handler should be used in that
case. This is traditionally done using the SetHandler/AddHandler
directives. See:
https://www.dscpl.com.au/wiki/ModPython/Articles/
SetHandlerVersusAddHandler
for more information.
> Based on this considerations, I have some questions:
> 1 - There is a way to call some code only when modpython is called
> for the
> first time ?
The PythonImport directive allows one to specify modules that should
be imported at the time that the Apache child process is first created
and prior to any actual request being processed by that process.
> I have to load a huge configuration file, and I'd like to do
> it only once and share the loaded object with all the handlers.
It isn't that simple. If you do such a thing only at process start
and as a
direct side effect of the import and the import fails but you always
expect
it to work, the only way to recover is by restarting Apache.
In general it is still better to wait until the first request arrives
and perform
the import at that point, but not as a side effect of the import, but by
calling a function which performs the load of any configuration data.
If a failure occurs, you can flag the loading of the configuration as
having
failed in some way and thus retry it on subsequent requests in case the
failure was caused by a transient problem such as a database issue etc.
When mod_python 3.3 is available you will be able to mix the two
approaches by using a new ability in 3.3 to have a function optionally
called after PythonImport is used to import the module. This avoids
issues
with failure during the actual import and more easily allows one to
recover from a failure.
You still have to be a bit careful where automatic reloading of modules
is enabled. This is because in current versions of mod_python data
can be overwritten and lost or simply lost on reloads. In mod_python 3.3
there is a proper way of maintaining data across reloads. This would
allow one to possibly avoid reloading the configuration if code files
giving access to it are changed.
> 2 - I have to implement a simple MVC framework that uses a mechaninc
> similar to the Publisher handler. But I need to interact with the
> object
> that will be published, capturing the data returned by some
> functions and
> sharing singleton among them.
The simplest thing to do is to pass the mod_python request object
between
functions. You can store references to other data in the request object.
If the object you put in the request object is a transient object it
will be
automatically reclaimed when the request processing is finished.
> Previously I loaded the files using exec but
> now I can't do that due to this reasons. Using import (or the imp
> module)
In mod_python, if you need to load modules from specific locations,
you should use apache.import_module(). See:
https://www.dscpl.com.au/wiki/ModPython/Articles/
BasicsOfModuleImporting
Module importing does have some problems with current versions of
mod_python though. See:
https://www.dscpl.com.au/wiki/ModPython/Articles/
ModuleImportingIsBroken
Module importing is fixed up in mod_python 3.3.
> is not the right way because I'd like the memory used by the
> controllers
> to be freed when the execution stops. There is a way to do that ?
It depends on to what level you expect to be able to free memory. Python
will obviously free up some stuff for you automatically, but if you
expect
to be able to unload a module then no. You will need to be more specific
about your expectations. See my above comment about using the request
object as the means to provide access to stuff that any code used to
service a request needs.
> 3 - Modpython starts a subinterpreter for every call to the handler ?
No, mod_python does not start a sub interpreter for every call. Once an
interpreter instance is created it is retained for the life of the
Apache
child process. There can still be multiple interpreter instances in a
process. These might be assigned against different virtual hosts or
parts
of the URL namespace. Situations in which distinct interpreters are
created are controlled by PythonInterpreter, PythonInterPer* directives.
For a bit more information on the process/interpreter model of Apache
and mod_python read:
https://www.dscpl.com.au/wiki/ModPython/Articles/
TheProcessInterpreterModel
Graham