[mod_python] About module autoreload

Graham Dumpleton grahamd at dscpl.com.au
Sun Feb 20 03:05:25 EST 2005

A few other comments about what you are trying to do.

On 20/02/2005, at 6:07 PM, Gonzalo Sainz-Trápaga wrote:

> Hi,
> After reading the FAQ entry 3.1, i finally built my custom way to 
> import
> modules, on the lines of:
> -- config.py --
> debug = True
> --
> -- anyscript.py --
> from mod_python.apache import import_module
> config = import_module('config')
> if config.debug:
>     # reload other modules
> --
> This is a bit lengthy and annoying but then i found this directive in
> the 3.x docs:
> -- http://www.modpython.org/live/current/doc-html/dir-other-par.html --
> PythonAutoReload {On,Off}
> (...)
> By default, mod_python checks the time-stamp of the file and reloads 
> the
> module if the module's file modification date is later than the last
> import or reload. This way changed modules get automatically 
> reimported,
> eliminating the need to restart the server for every change.
> --
> If I explicitly set this directive to On, i get the expected behaviour
> without manually reloading modules on every script. But, if i don't set
> it i get the "Off" behaviour, instead of the "On" one as advertised in
> the docs.

That can't be true. Code where top level module loading occurs is:

             module = import_module(module_name,
autoreload=int(config.get("PythonAutoReload", 1)),

Thus, default is definitely "On" or enabled.

> Actually I'm glad this directive exists and seems to do what I think it
> does, but the FAQ must *really* be updated on this, and, if i'm not
> mistaken, the default behaviour should be corrected either in the docs
> or in mod_python (i would suggest the former, as automatically 
> reloading
> modules is the standard interpreter behaviour).
> I wanted to check with you guys before I mess with the FAQ, in case i'm
> getting something wrong.

As well as pointing out that PythonAutoReload doesn't apply to your 
use of import_module(), it is also worthwhile point out that if your 
modules" are changed, they will not actually be automatically reloaded 
in your
example unless the top level file which imports them is also changed.

To try and make this a bit clearer, imagine your top level module is A 
and it
imports a submodule call B. If you change A, if PythonAutoReload is On, 
A will be reloaded. B will not however be reloaded at that time but 
will be
taken from the cache. This is because B hadn't been changed.

Now, if you modify B but not A, nothing will happen at all. Ie., 
neither A or
B will be reloaded. This is because the mod_python module import and 
system makes no attempt to detect that A needs reloading because of B 
B will only be reloaded, if A is changed, thus forcing the re-execution 
of the
import_module() function on B.

This and other problems in import_module() mean it is a pain to use and 
people resort to schemes such as touching all Python code files every 
they make a change to one, just to ensure everything that potentially 
reloading is reloaded. This does though mean that things that didn't 
reloading also get reloaded.

In short, import_module() is broken. If you are curious about other 
schemes, you might look at Vampire, which implements an import 
mechanism which
does detect changes in subimports and will automatically reimport 
parents as
necessary. Vampire is at "http://www.dscpl.com.au/projects/vampire".


More information about the Mod_python mailing list