Graham Dumpleton
grahamd at dscpl.com.au
Mon Dec 11 05:36:25 EST 2006
The module importer has been rewritten in mod_python 3.3 and addresses the various problems earlier versions had. See: http://www.dscpl.com.au/wiki/ModPython/Articles/ ModuleImportingIsBroken for a run down of the problems. You can find out a bit about the capabilities of the new importer by reading documentation for apache.import_module() at: http://svn.apache.org/viewvc/httpd/mod_python/trunk/Doc/ modpython4.tex?view=markup Version 3.3 of mod_python is in final round of checks before proper release at this very moment. Graham On 11/12/2006, at 8:28 PM, durumdara wrote: > Hi! > > I many times thinking about better site structures, and everytime I > hampered by "import hell"... > So yesterday I tried something. > > There is many way to avoid this problem. > With import: > 1.) Use only one file that handle every request. > 2.) Use one main file, and one file for every subdirs. > 3.) Use many file, every of them handle it's subdirs. > In this way you need unique named submodules: xxxx_name (where xxxx > is a hexad. id), or fullpathname (example: /main/sub/subsub = > main_sub_sub). > This structure is very unflexible, because some of the informations > are stored in real subfolders, some of in the filenames. > I cannot create unified directory structure for use same names > except I create unique module names. > > Without import: > 1.) Execfile, Exec. > 2.) PSP. > > Exec is good, but need reinterpretation in every request, and every > of them need to have same structure. > > The conclosion that import is need UNIQUE name, but uniqueness is > could be everything what can representable in complete filepath. > > Yesterday I tried something. > The module importing in python is based on sys.modules. > But if you get out from this way, you can use imp module to do it > in your way. > > I created some example. > In this example I create many subdirs with many index.py, and I > import them one by one. > And later I modify one of them, and I test reloading. > It is working, but some os.sleep is needed to it recognize the > changes. > > Please write me (Graham!) your opinion: it is useful, or not ? > > Thanks for it: > dd > > import os, sys > from threading import Lock > > _ImportLock = Lock() > _Modules = {} > > def Import(ModPath): > ModPath = os.path.abspath(ModPath) > _ImportLock.acquire(1) > try: > if not os.path.exists(ModPath): > raise Exception, 'Module named "%s" does not exists!' % > ModPath > modinfo = _Modules.get(ModPath, None) > needreload = (not modinfo) > actmodtime = os.path.getmtime(ModPath) > if modinfo: > modtime, module = modinfo > needreload = (actmodtime != modtime) > if not needreload: > return module > mpath, mfn = os.path.split(ModPath) > mf, me = os.path.splitext(mfn) > import imp > imp.acquire_lock( ) > try: > afile, apathname, adescription = imp.find_module(mf, > [mpath]) > module = imp.load_module(mf, afile, ModPath, adescription) > del(sys.modules[mf]) > finally: > imp.release_lock() > _Modules[ModPath] = [actmodtime, module] > return module > finally: > _ImportLock.release() > > > #print os.path.abspath('./t1/index.py') > #import sys > #sys.exit() > modtext = ''' > name = %s > def PrintInfo(): > print "File:", __file__ > print "Name var:", name > > ''' > moddatas = [] > > def WriteIndexPy(Number, VarValue): > spath = './subdir%03d' % Number > if not os.path.exists(spath): > os.makedirs(spath) > sfn = os.path.join(spath, 'index.py') > pytext = modtext % VarValue > f = open(sfn, 'w') > f.write(pytext) > f.close() > > print "Creating modules" > for i in range(100): > WriteIndexPy(i, i) > > print "Import new modules" > import time > st = time.time() > for i in range(100): > spath = './subdir%03d' % i > sfn = os.path.join(spath, 'index.py') > m = Import(sfn) > #print "Module:", m > #print "Module's var", m.name > #m.PrintInfo() > et = time.time() > print et - st, "seconds" > > print "Import loaded modules" > import time > st = time.time() > for i in range(100): > spath = './subdir%03d' % i > sfn = os.path.join(spath, 'index.py') > m = Import(sfn) > #print "Module:", m > #print "Module's var", m.name > #m.PrintInfo() > et = time.time() > print et - st, "seconds" > > time.sleep(1) > print "Reload 1" > spath = './subdir%03d' % 0 > sfn = os.path.join(spath, 'index.py') > WriteIndexPy(0,100) > m = Import(sfn) > print "Module:", m > print "Module's var", m.name > m.PrintInfo() > > print "Reload 2" > m = Import(sfn) > print "Module:", m > print "Module's var", m.name > m.PrintInfo() > > print "Exec test" > import time > st = time.time() > for i in range(100): > spath = './subdir%03d' % i > sfn = os.path.join(spath, 'index.py') > execfile(sfn, globals(), locals()) > et = time.time() > print et - st, "seconds" > > > > > > import os, sys > from threading import Lock > > _ImportLock = Lock() > _Modules = {} > > def Import(ModPath): > ModPath = os.path.abspath(ModPath) > _ImportLock.acquire(1) > try: > if not os.path.exists(ModPath): > raise Exception, 'Module named "%s" does not exists!' % > ModPath > modinfo = _Modules.get(ModPath, None) > needreload = (not modinfo) > actmodtime = os.path.getmtime(ModPath) > if modinfo: > modtime, module = modinfo > needreload = (actmodtime != modtime) > if not needreload: > return module > mpath, mfn = os.path.split(ModPath) > mf, me = os.path.splitext(mfn) > import imp > imp.acquire_lock( ) > try: > afile, apathname, adescription = imp.find_module(mf, > [mpath]) > module = imp.load_module(mf, afile, ModPath, adescription) > del(sys.modules[mf]) > finally: > imp.release_lock() > _Modules[ModPath] = [actmodtime, module] > return module > finally: > _ImportLock.release() > > > #print os.path.abspath('./t1/index.py') > #import sys > #sys.exit() > modtext = ''' > name = %s > def PrintInfo(): > print "File:", __file__ > print "Name var:", name > > ''' > moddatas = [] > > def WriteIndexPy(Number, VarValue): > spath = './subdir%03d' % Number > if not os.path.exists(spath): > os.makedirs(spath) > sfn = os.path.join(spath, 'index.py') > pytext = modtext % VarValue > f = open(sfn, 'w') > f.write(pytext) > f.close() > > print "Creating modules" > for i in range(100): > WriteIndexPy(i, i) > > print "Import new modules" > import time > st = time.time() > for i in range(100): > spath = './subdir%03d' % i > sfn = os.path.join(spath, 'index.py') > m = Import(sfn) > #print "Module:", m > #print "Module's var", m.name > #m.PrintInfo() > et = time.time() > print et - st, "seconds" > > print "Import loaded modules" > import time > st = time.time() > for i in range(100): > spath = './subdir%03d' % i > sfn = os.path.join(spath, 'index.py') > m = Import(sfn) > #print "Module:", m > #print "Module's var", m.name > #m.PrintInfo() > et = time.time() > print et - st, "seconds" > > time.sleep(1) > print "Reload 1" > spath = './subdir%03d' % 0 > sfn = os.path.join(spath, 'index.py') > WriteIndexPy(0,100) > m = Import(sfn) > print "Module:", m > print "Module's var", m.name > m.PrintInfo() > > print "Reload 2" > m = Import(sfn) > print "Module:", m > print "Module's var", m.name > m.PrintInfo() > > print "Exec test" > import time > st = time.time() > for i in range(100): > spath = './subdir%03d' % i > sfn = os.path.join(spath, 'index.py') > execfile(sfn, globals(), locals()) > et = time.time() > print et - st, "seconds" > > import os, sys > from threading import Lock > > _ImportLock = Lock() > _Modules = {} > > def Import(ModPath): > ModPath = os.path.abspath(ModPath) > _ImportLock.acquire(1) > try: > if not os.path.exists(ModPath): > raise Exception, 'Module named "%s" does not exists!' % > ModPath > modinfo = _Modules.get(ModPath, None) > needreload = (not modinfo) > actmodtime = os.path.getmtime(ModPath) > if modinfo: > modtime, module = modinfo > needreload = (actmodtime != modtime) > if not needreload: > return module > orgsyspath = sys.path[:] > try: > mpath, mfile = os.path.split(ModPath) > mfn, mfe = os.path.splitext(mfile) > for apath in orgsyspath: > amodpath = os.path.join(apath, mfile) > if os.path.exists(amodpath): > sys.path.remove(apath) > sys.path.append(mpath) > if sys.modules.has_key(mfn): > del(sys.modules[mfn]) > module = __import__(mfn) > sys.path.remove(mpath) > del(sys.modules[mfn]) > _Modules[ModPath] = [actmodtime, module] > return module > finally: > sys.path = orgsyspath[:] > finally: > _ImportLock.release() > > > #print os.path.abspath('./t1/index.py') > #import sys > #sys.exit() > modtext = ''' > name = %s > def PrintInfo(): > print "File:", __file__ > print "Name var:", name > > ''' > moddatas = [] > for i in range(100): > spath = './subdir%03d' % i > if not os.path.exists(spath): > os.makedirs(spath) > sfn = os.path.join(spath, 'index.py') > pytext = modtext % i > open(sfn, 'w').write(pytext) > import gc > gc.collect() > import time > st = time.time() > for i in range(100): > spath = './subdir%03d' % i > sfn = os.path.join(spath, 'index.py') > m = Import(sfn) > #moddatas.append([m, m.name]) > #print "Module:", m > #print "Module's var", m.name > #m.PrintInfo() > et = time.time() > print et - st, "seconds" > > ''' > m1 = Import('./t1/index.py') > print m1.name > m2 = Import('./t2/index.py') > print m2.name > m2a = Import('./t2/index.py') > print m2a.name > ''' > #import sys > #print sys.modules > #print "mod1" > #print globals() > #print locals() > #print a > # > #a = 2 > > def main(RO): > x > > import os, sys > > a = {'a' : 1} > #locals() > #print sys.modules.keys() > execfile('execmod1.py', a) > print a['a']_______________________________________________ > Mod_python mailing list > Mod_python at modpython.org > http://mailman.modpython.org/mailman/listinfo/mod_python
|