[mod_python] Force reload?

Shawn Harrison harrison at tbc.net
Thu Feb 3 22:49:19 EST 2005


Shawn Harrison wrote [02/03/05 1:30 PM]:
> I should have mentioned that you could also just use PythonDebug On 
> and then you're using req.get_config(), and this would be global. 
> Whereas if you define your own PythonOption you can turn on/off 
> debugging for different parts of your application and you'll be using
>  req.get_option().
> 

First of all, I should introduce myself before continuing with this
thread. I joined the list this past week; I've been actively developing
with mod_python for about a year and loving it. I'm a project manager by
day, a web and database developer by night.

I'm interested in this discussion about forcing reload, because I've
been using "apachectl restart" to handle this issue and am ready to do
something better.

David Geller wrote [02/03/05 1:34 PM]:
> The way that python handles import is problematic for persistent 
> interpreters such as implemented by mod_python. There doesn't, to me,
>  seem any foolproof, *simple* way to solve reimportation issues in 
> all circumstances, EXCEPT for what Daniel proposes, other than by 
> restarting the server. It is such a pain to make some source changes,
>  *think* you have reloaded affected modules, and then find out that 
> the changes haven't taken effect! I have used this method for awhile
>  now, and for debugging/development it seems to work ...

I'm curious about this. I just whacked up the following function over
lunch today, following my own advice, and tested it a bit just now. The 
function seems to work just fine in all the use cases (though I didn't 
do formal unit testing).

Is there any reason I'm not seeing why this function won't solve the 
reload problem, if used in place of all import statements in the 
application?

def import_(modname, req=None, pythonopt=None, reloadp=False):
     """Imports the given module and, if directed, reloads it.
         (Required:)
             modname   -- the name of the module to import
         (Optional:)
             req       -- a mod_python req object
                          (needed to check Apache directives)
             pythonopt -- the name of a PythonOption debug flag
                          for your app
             reloadp   -- a direct boolean instruction to reload or not.

         If a True value is given for reloadp, the module will be
         reloaded. Otherwise, if only the module name is given, the
	module will only be imported and not reloaded. But if req is
	given, it will be used to check the Apache directives to see
	whether reload() should be done. If pythonopt is also given, the
	value of the named PythonOption will be used (The PythonOption
	can be named anything; it must have the value 'On' or 'True' or
	'1' in order to be True); otherwise, the value of PythonDebug
	will be used if it exists. (The value of PythonDebug is either
	'1' or '0'.)
     """

     # shamelessly stolen from the Python documentation....
     mod = __import__(modname)
     components = modname.split('.')
     for comp in components[1:]:
         mod = getattr(mod, comp)

     # decide whether to reload()
     if req:
         if pythonopt and req.get_options().has_key(pythonopt):
             opt = req.get_options()[pythonopt]
             if opt == 'On' or opt == 'True' or opt == '1':
                 reloadp = True
         elif req.get_config().has_key('PythonDebug'):
             if req.get_config()['PythonDebug'] == '1':
                 reloadp = True
     if reloadp == True:
         reload(mod)

     return mod

# Tested with Apache/2.0.52 (Win32) mod_python/3.1.3 Python/2.3.4 Server
-- 
Shawn Harrison
________________
harrison at tbc.net



More information about the Mod_python mailing list