|
durumdara
durumdara at gmail.com
Mon Dec 11 04:28:59 EST 2006
Skipped content of type multipart/alternative-------------- next part --------------
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"
-------------- next part --------------
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
'''
-------------- next part --------------
#import sys
#print sys.modules
#print "mod1"
#print globals()
#print locals()
#print a
#
#a = 2
def main(RO):
x
-------------- next part --------------
import os, sys
a = {'a' : 1}
#locals()
#print sys.modules.keys()
execfile('execmod1.py', a)
print a['a']
|