[mod_python] Stopping Reimports

Nicolas Lehuen nicolas at lehuen.com
Tue Aug 10 16:27:58 EDT 2004


Hi,

I don't think your solution is so ugly... A few weeks ago I wrote about a
similar problem on this list, though my solution was to alter publisher.py
to change the way it builds module names (publisher.py version 1.36, line 67
in mod_python 3.1.3)... Or, as Daniel Popowich wrote, you could use
mod_servlet (http://home.comcast.net/~d.popowich/mpservlets/) which does
correctly load modules.

Note that you were in fact quite lucky to experience reimports, since some
people (including me, running mp 3.1.3 on Apache2 multithreaded on Windows)
experience a much worse behaviour : no reimport at all, e.g. /foo/index.py
would return the same page as /bar/index.py if /bar/index.py was touched
after /foo/index.py !

Regards,

Nicolas Lehuen

> -----Message d'origine-----
> De : mod_python-bounces at modpython.org 
> [mailto:mod_python-bounces at modpython.org] De la part de Cu _
> Envoyé : mardi 10 août 2004 13:47
> À : mod_python at modpython.org
> Objet : [mod_python] Stopping Reimports
> 
> 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