[mod_python] Newbie Questions

Graham Dumpleton grahamd at dscpl.com.au
Mon Nov 13 16:01:59 EST 2006

Please keep the discussion on the mod_python mailing list.

g.farina at pharaondev.com wrote ..
> Thanks
> your answers really helped me a lot. I think I'll pass the request object
> (or better a wrapper over it) to the controllers. then I have only to be
> sure that the controllers does not retain references (but week) to this
> object to be sure it is free automatically by mod_python.

The need to not hold references to the request object beyond the
life of the request is actually quite important. This is because the
internal Apache structures which it wraps will be destroyed regardless
at the end of the request. If you keep around the request object
and after the request has completed try to access it, you can
potentially crash the Apache child process.

> Another shourt question: how can I get the content-type of a request ?
> Using req.content_type or this property is used only for writing the
> response content-type ?

The req.content_type attribute will be populated by Apache for
a request based on what it can determine about the type of
resource being requested. For example, if you are using PSP
and have added to your Apache configuration:

  AddType text/html .psp

so that Apache knows that a request of a .psp file will result in
return of 'text/html' content, then req.content_type will already
be set for you before your response handler is run. I can't
remember what it sets it to if it can't work it out. It may even set
it to the value of the DefaultType directive already at that point.

The only tricky case is if the request mapped to a directory and
you are using SetHandler directive. In that case the value of
req.content_type will be 'httpd/unix-directory' regardless of
whether UNIX of Windows is used.

> > 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.
> I need to unload the controller (module) and be sure that all the
> submodules used are unloaded the same way.
> Any suggestions ?

Using apache.import_module() at least there is no way to unload a module
and in general it would be a very hard thing to achieve and potentially
dangerous to attempt depending on what your application does.

My question to you would be why you think you need to unload it? Having
to unload it every time would impact the performance and makes the idea
that the interpreter is persistent between requests allowing caching, to be
a pointless feature. If you want to unload stuff, you may as well just use CGI.

Note that if you want to unload it because you want any changes to code
to be picked up automatically, mod_python does have automatic module
reloading. It is a bit broken in current mod_python and you need to be
careful how you use it, but fixed in mod_python 3.3.


More information about the Mod_python mailing list