[mod_python] [patch] make autoreload more useful

Dustin Mitchell dustin at ywlcs.org
Wed May 29 07:45:05 EST 2002

On Wed, May 29, 2002 at 06:24:40PM +1000, Martin Pool wrote:

> +# The basic idea is that we want to delete everything from sys.modules
> +# when the user's code changes, except for system modules.  This is
> +# necessary because otherwise one module that has not changed may
> +# still be holding references to code objects from a module that has
> +# changed.  We have to actually delete them and start again rather
> +# than using reload(), because otherwise the first one reloaded may
> +# grab references from something that will be reloaded later on.

Two things:

1. I'm not sure how gc interacts with this, but there was a time when you
just couldn't get rid of a loaded module object.  All of the modules you
delete will still reference each other in some sort of cycle. If they're not
deleted you could very quickly end up with an enormous httpd process.

Even with gc, if there are any dangling references into one of the old
modules (an atexit function? somewhere the Python standard libraries cache a
reference?) then you're going to have a bloated httpd.

I wrote a reloader similar to this one, and my solution to this was to watch
the number of reloads, and call req.child_terminate() after a certain number
(I picked 10) or reloads.

2. One other difficulty: some modules actually *do* want to have global
state.  For example, a MySQL interface should be able to establish its
connection once and maintain that through many requests.  Perhaps there
should be a mechanism for a module to say "don't remove me!", or at least
"preserve the value of this variable".



  Dustin Mitchell
  dustin at ywlcs.org

More information about the Mod_python mailing list