ModuleManager reloads only if file's mtime changed
This commit is contained in:
@ -13,24 +13,40 @@ class ModuleManager:
|
||||
def __init__(self, folder):
|
||||
self.folder = Path(folder)
|
||||
self.modules = {}
|
||||
self.mtimes = {}
|
||||
self.update()
|
||||
|
||||
|
||||
def update(self):
|
||||
for fn in self.fnames:
|
||||
print(fn, fn.stat().st_mtime)
|
||||
name = fn.stem
|
||||
mtime = fn.stat().st_mtime
|
||||
print(fn, name, mtime)
|
||||
|
||||
if name in self.mtimes:
|
||||
if self.mtimes[name] == mtime:
|
||||
print(f"{name} unchanged... will not reload")
|
||||
continue
|
||||
else:
|
||||
print(f"{name} changed... reloading")
|
||||
|
||||
self.mtimes[name] = mtime
|
||||
|
||||
try:
|
||||
mod = load_module(name, fn)
|
||||
except Exception as e:
|
||||
print(f"loading {name} raised", printable_exception(e))
|
||||
continue
|
||||
|
||||
try:
|
||||
func = mod.run
|
||||
except AttributeError:
|
||||
print(f"missing run function in {name}")
|
||||
continue
|
||||
|
||||
self.modules[name] = func
|
||||
|
||||
|
||||
@property
|
||||
def fnames(self):
|
||||
fns = self.folder.glob("*.py")
|
||||
@ -43,6 +59,7 @@ modman = ModuleManager(folder)
|
||||
mods = modman.modules
|
||||
|
||||
while True:
|
||||
modman.update()
|
||||
with ThreadPoolExecutor() as ex:
|
||||
for f in mods.values():
|
||||
fut = ex.submit(f)
|
||||
|
Reference in New Issue
Block a user