[mod_python] problem with auto_reload in apache.py

Brian Hawthorne brianh at exelixis.com
Wed Mar 21 15:37:50 EST 2001


marc, i had very similar and equally strange problems with the
module reloading; if for example 3 different versions of the module had
been loaded since the last apache restart, it would use them in a random
repetitive sequence, like  12231321133313211223132112231321... etc.
_very_ strange.  i fixed the problem with some changes to the
mod_python.apache.import_module function.  i've included my improved
version below.

hope it helps!
brian

#---------------------------------------------------------------------
def import_module(module_name, req=None, path=None):
    """ 
    Get the module to handle the request. If
    autoreload is on, then the module will be reloaded
    if it has changed since the last import.
    """

    # get the options
    autoreload, debug = 1, None
    if req:
        config = req.get_config()
        autoreload = not config.has_key("PythonNoReload")
        debug = not config.has_key("PythonDebug")

    # try to import the module

    oldmtime = None
    mtime = None

    if  not autoreload:

        parts = string.split(module_name, '.')
        for i in range(len(parts)):
            f, p, d = imp.find_module(parts[i], path)
            try:
                mname = string.join(parts[:i+1], ".")
                module = imp.load_module(mname, f, p, d)
            finally:
                if f: f.close()
            if hasattr(module, "__path__"):
                path = module.__path__

        # import module
        #module = __import__(module_name)
        #components = string.split(module_name, '.')
        #for cmp in components[1:]:
        #    module = getattr(module, cmp)

    else:

        # keep track of file modification time and
        # try to reload it if it is newer
        if sys.modules.has_key(module_name):

            # the we won't even bother importing
            module = sys.modules[module_name]

            # does it have __mtime__ ?
            if sys.modules[module_name].__dict__.has_key("__mtime__"):
                # remember it
                oldmtime = sys.modules[ module_name ].__mtime__

        # import the module for the first time
        else:

            parts = string.split(module_name, '.')
            for i in range(len(parts)):
                f, p, d = imp.find_module(parts[i], path)
                try:
                    mname = string.join(parts[:i+1], ".")
                    module = imp.load_module(mname, f, p, d)
                finally:
                    if f: f.close()
                if hasattr(module, "__path__"):
                    path = module.__path__

##            module = __import__(module_name)
##            components = string.split(module_name, '.')
##            for cmp in components[1:]:
##                module = getattr(module, cmp)

        # find out the last modification time
        # but only if there is a __file__ attr
        if module.__dict__.has_key("__file__"):

            filepath = module.__file__

            if os.path.exists(filepath):

                mod = os.stat(filepath)
                mtime = mod[stat.ST_MTIME]

            # check also .py and take the newest
            if os.path.exists(filepath[:-1]) :

                # get the time of the .py file
                mod = os.stat(filepath[:-1])
                mtime = max(mtime, mod[stat.ST_MTIME])

    # if module is newer - reload
    if (autoreload and (oldmtime < mtime)):
        del sys.modules[module_name]
        # import the module as if for the first time
        parts = string.split(module_name, '.')
        for i in range(len(parts)):
            f, p, d = imp.find_module(parts[i], path)
            try:
                mname = string.join(parts[:i+1], ".")
                module = imp.load_module(mname, f, p, d)
            finally:
                if f: f.close()
            if hasattr(module, "__path__"):
                path = module.__path__

        #try: module = reload(module)
        #except: pass

    # save mtime
    module.__mtime__ = mtime

    return module
#---------------------------------------------------------------------

On Wed, 21 Mar 2001, Marc Fiuczynski wrote:

> Hi, 
> 
> apache.py seems to be having trouble reloading my python modules when I
> update them.  This happens when apache is running with mod_python, and I
> then change my python module.  It fails in several odd ways:
> 
> 1) after I change my python module, mod_python will work every other time
> that I use it via my browser.
> 2) after I change my python module, mod_python completely fails to reload
> it.
> 
> In either case, if I send a HUP signal to the apache server, then everything
> works just fine afterwards.
> 
> 
> 
> My configuration is as follows:
> *	python module is being invoked via mod_python's publisher interface.
> *	mod_python 2.7.2
> *	python 2.0
> *	apache 1.3.14
> *	Red Hat Linux 7.0
> 
> I have apache configured with the following:
>   AddHandler python-program .py
>   PythonHandler mod_python.publisher
>   PythonDebug On
>   PythonNoReload Off
> 
> Can anyone provide some insight?!
> 
> Thanks,
> Marc
> 
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://www.modpython.org/mailman/listinfo/mod_python
> 




More information about the Mod_python mailing list