[mod_python] Stopping Reimports

Gregory (Grisha) Trubetskoy grisha at modpython.org
Tue Aug 10 11:27:17 EDT 2004


The solution is to not have PythonDebug enabled - or do you need that for 
some reason?

Grisha

On Tue, 10 Aug 2004, Cu _ wrote:

> mpy
> Hello,
>
> I'd like to know if there is some elegant way to deal with the
> [notice] mod_python: (Re)importing module 'index' with path set to [...]
>
> The mod_python.apache importing mechanism works correctly, the problem is,
> it keeps reimporting the same files.
>
> I don't think reimporting is the best solution for modules with same names but in different directories.
>
> It degrades performance with unneeded imports.
>
> Then,
> I try to use mod_python's sideeffect of not discarding variables when importing already
> loaded modules to cache frequently used data (If the module doesn't get reimported all the time, it's variables keep their values between subsequent requests).
> Every reimport causes the cached data to be reloaded.
>
> Also, it pollutes error_log :)
>
> My current "solution" is ugly, that's why I post here, maybe somebody will come up with something
> better.
> The idea is - if i have to load a module "my_module", which is at /path/to/my_files,
> don't load it as my_module, but as path_to_my_files_my_module
> That way, modules with same names will be assigned different names in sys.modules and won't
> get reloaded all the time.
>
> It's as using
> from path.to.my.files imort my_module as path_to_my_files_my_module
> instead of
> from path.to.my.files imort my_module
>
> Modules loaded with import_module are usually used by pointers, not by symbolic names so, as for
> me, it's ok to load it with a different name.
>
> my mod_python/apache.py import_module() now looks like this:
>
> def import_module(module_name, autoreload=1, log=0, 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.
>    """
>
>    parts = module_name.split('.')
>    if path:
>      path2 = '_'.join(path).replace('/','_').replace('.','_').replace('-','_')[1:] + '_'
>      mn = path2 + module_name
>    else:
>      mn = module_name
>
>    # (Re)import
>    import sys
>    if sys.modules.has_key(mn):
>        module = sys.modules[mn]
>    else:
>
>        _apache.log_error('Has to load: '+str(mn)+ '('+str(module_name)+')', APLOG_NOERRNO|APLOG_NOTICE)
>
>        for i in range(len(parts)):
>            mname = ".".join(parts[:i+1])
>            f, p, d = imp.find_module(parts[i], path)
>            try:
>                _apache.log_error('Loading: '+mname, APLOG_NOERRNO|APLOG_NOTICE)
>                module = imp.load_module(mname, f, p, d)
>
>            finally:
>                if f: f.close()
>            if hasattr(module, "__path__"):
>                path = module.__path__
>
>        # dark magic
>        if mn:
>          if mn != mname:
>            t = sys.modules.keys()
>            t.sort()
>            _apache.log_error('Current load: '+str(t), APLOG_NOERRNO|APLOG_NOTICE)
>            _apache.log_error('Switching: '+mname+' <-> '+mn, APLOG_NOERRNO|APLOG_NOTICE)
>            sys.modules[mn] = sys.modules[mname]
>            del(sys.modules[mname])
>
>            t = sys.modules.keys()
>            t.sort()
>            _apache.log_error('After switch: '+str(t), APLOG_NOERRNO|APLOG_NOTICE)
>
>    return module
>
> -------------------------------------------------------------------------------
> http://www.one.lv - Tavs mobilais e-pasts!
>
> Tagad lasi savu e-pastu ar mobilo telefonu - wap.one.lv!
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python
>


More information about the Mod_python mailing list