[mod_python] Howto force reload of all modules

Graham Dumpleton graham.dumpleton at gmail.com
Fri Aug 31 18:20:19 EDT 2007

On 01/09/2007, hhanke at pdx.edu <hhanke at pdx.edu> wrote:
> Quoting Graham Dumpleton <graham.dumpleton at gmail.com>:
> > On 31/08/2007, hhanke at pdx.edu <hhanke at pdx.edu> wrote:
> >> please, I'm using apache.import_module() to import my modules
> >> in mod_python 3.2.8 (I'm stuck with this version on our
> >> production server). Since it has its problems, please, what
> >> is the easiest way to reload all modules?
> > The only reliable way is to restart the whole of Apache.
> This just cannot be done on a production sever where multiple
> websites run and a server I as a developer have no controll about.
> > If it were a development server, you could also use the brute force
> > method of having the Apache child process restart after every request.
> > This is done using the Apache directive:
> >   MaxRequestsPerChild 1
> > This should however never be used on a production server, as it would
> > give CGI like performance.
> It would be acceptable to use it for a short period, e.g. after updating
> the website to the new release version, but since I can't do that on
> a per-directory basis through .htaccess, it is of no use on a production
> server where multiple websites run and I can't edit the global conf.
> > In short, there is no good way of doing it.
> So how do I use Python for writing more complicated webpages,
> that need to use modules (both my own and completely external
> that I have no controll about and they use the classical import)?
> Do I have to use CGI then?
> > Even the module importer in 3.3.1 is only of use to web application
> > code which is managed by mod_python and not normal Python modules
> > on sys.path.
> Hm... Can I somehow force the reload of a given module that I imported
> through apache.import_module manually? I mean an equivalent of reload().

Have you read the documentation for 'import_module()' in mod_python 3.3.1?


The whole point of 'import_module()' is that provided the modules are
ones which mod_python can manage (not on sys.path), then when you make
changes to a module, or a module which is depended upon by another
module, the top level module and any modules on the path down to the
changed module will be automatically reloaded.

But then, if you cant upgrade to 3.3.1 you will be stuck with the
older implementation of 'import_module()' which has lots of problems
and doesn't handle reloading when child modules are changed. See:


If CGI is an option, because you aren't really using mod_python
specific features anyway, then yes you could use CGI, but in that case
you would better off converting your application to be a WSGI
application and using mod_wsgi (www.modwsgi.org) with its daemon mode.
In daemon mode of mod_wsgi your application is delegated to its own
separate process and you can control restarts of just that process
using signals. This allows one to implement an automatic reloader.


In summary, with mod_python your production box is best used only for
deployment and not for development. You can only get so far using
'import_module()' properly on a production box, sometimes you just
cant avoid doing a restart. Thus you really need to use a separate
Apache instance when you are doing development with mod_python if you
have no ability to restart your production server.


More information about the Mod_python mailing list