[mod_python] module reloading

David MacQuigg david_macquigg at yahoo.com
Fri Jul 21 12:45:41 EDT 2006

Sorry for the late reply.  I posted this last week, but made a mistake in 
using the wrong return address, which resulted in the message being 
rejected without notice.  I also got no response from 
mod_python-owner at modpython.org

At 08:46 PM 7/13/2006 -0400, you wrote:

>I'm having issues with ye olde module reloading.
>i.e. modules don't reload, need to restart server, yadda yadda yadda.
>Even touching all my .py and .pyc files doesn't seem to help... it
>still seems to require a restart.
>I see there have been frequent posts about this issue on this list,
>and it sounds like it's not quite resolved yet.
>I'm wondering if there's a simple line or two of code I can add to my
>entry point, before any importing happens, which will clean out the
>module cache? Just unload everything to force a reload of everything?
>That would work quite well for me during development.

I know very little about mod_python, but let me speculate based on my 
understanding of Python reloads.  There are discussions of module 
importing/reloading in "Learning Python, 2nd ed. pp.266-269, and in "Python 
in a Nutshell", pp.120-123.  I've also put what I hope is a more clear 
presentation for students at http://purl.net/macquigg/Python/Misc/Reload.htm

You could "clean out the module cache" by deleting everything in 
sys.modules.  That would trick your import statements to reload their 
modules instead of referencing existing copies.  You should do this 
explicitly with the reload() function, however, and avoid unintended 
deletion of other modules.

What you are asking for (a general command 'clean out everything but system 
stuff') may be practically impossible.  Take a look at sys.modules.keys(), 
even before loading any of your own modules.  There is a lot of stuff 
there.  The interpreter doesn't keep track of what objects in these modules 
have been referenced/copied/subclassed/instantiated/etc, and now have 
references (bindings) that keep them in memory until the last reference is 
gone, and the garbage collector can do its job.  Reloading a module creates 
new objects in memory, but leaves *as is* any old objects with active 
references.  Example:

import Module1
x1 = Module1.x
x2 = Module1.x

x2 now binds the updated object Module1.x, but the old object is still kept 
in memory bound to variable x1.

The only way to ensure a clean update, with no vestiges of an old module, 
is to restart the interpreter session, as IDLE does when you press key F5.

I don't know if it might be possible to restart mod_python without 
restarting Apache.  That could work if it were set up like a Sendmail 
milter.  I edit my milter frequently, and restarting it is easy, with no 
apparent disruption of Sendmail.  Sendmail milters interface via a socket, 
so maybe an "arms length" interface like this would be required between 
mod_python and Apache.

If you are working on a development system, '/etc/init.d/httpd restart' 
should not cause any disruption, and it seems to be just as fast as 
restarting Python.

-- Dave
************************************************************     *
* David MacQuigg, PhD      email: macquigg at open-mail.org   *  *
* President, Open-Mail dot org      phone: USA 520-721-4583   *  *  *
* Postmaster, Box67 dot com                                   *  *  *
*                                 9320 East Mikelyn Lane       * * *
* http://purl.net/macquigg        Tucson, Arizona 85710          *
************************************************************     *

More information about the Mod_python mailing list