re-importing modules

Graham Dumpleton Graham.Dumpleton at gmail.com
Tue May 1 01:28:49 EDT 2007


On May 1, 2:17 pm, Steven D'Aprano <s... at REMOVEME.cybersource.com.au>
wrote:
> On Tue, 01 May 2007 00:32:20 +0000, John Nagle wrote:
> > kyoso... at gmail.com wrote:
>
> >>>In addition to the warning that reload() does not recursively reload
> >>>modules that the reloaded module depends on, be warned that reloading a
> >>>module does not magically affect any functions or objects from the old
> >>>version that you may be holding on to.
>
> >     Maybe reloading modules should be deprecated.  The semantics
> > are awful, and it interferes with higher-performance implementations.
>
> I'd hate for reload to disappear, it is great for interactive
> development/debugging, at least under some circumstances. (If you have
> complex and tangled class hierarchies, it might not be powerful enough.)
>
> As for the semantics being awful, I disagree. reload() does exactly
> what it claims to do, no more, no less. I wouldn't expect reload(module1)
> to reload modules 2 through 5 just because they were imported by module1.
> If I wanted them reloaded, I'd say so.

That it doesn't reload a parent when a child changes may be fine in an
interactive debugger, but can cause problems if not done where
automatic reloading is being done in a long running web application
where you want to avoid server restarts. For that very reason, the new
importer in mod_python 3.3 tracks parent/child relationships between
modules and is able to import a parent module when a child changes.
The new importer does a lot more besides that as well, including
distinguishing modules by full path rather than purely by name. For
some details on the mod_python importer see documentation for
import_module() in:

  http://www.modpython.org/live/current/doc-html/pyapi-apmeth.html

To understand why the new importer does some of the things it does, it
may be helpful to read all the problems the previous importer was
causing:

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

Getting module importing with reloading working is frightfully tricky
in an application where multiple threads corresponding to concurrent
web requests are executing as one can't be reloading on top of
modules where code may be executing, one must also avoid triggering a
reload of a specific module half way through a request where the same
module is encountered twice and lots, plus lots of other gotchas.

Graham








More information about the Python-list mailing list