ModuleManager reloads only if file's mtime changed

This commit is contained in:
2021-02-06 13:46:49 +00:00
parent 296c091ef7
commit a3bc256c28

View File

@ -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)