Module.start raises on missing func; Module.kill kills Task if available; Module.get_mtime returns None of file is missing instead of raising error; added try/except/print to ModuleManager._run_all; added module killing when removing module from cache; slowed down the output

This commit is contained in:
2021-02-08 10:58:03 +00:00
parent 6d6b4e0d51
commit 8f849975c5
2 changed files with 27 additions and 11 deletions

View File

@ -13,7 +13,7 @@ class ModuleManager:
self.update() self.update()
def run(self, sleep_time=1): def run(self, sleep_time=2):
while True: while True:
print("-" * 20) print("-" * 20)
self.update() self.update()
@ -28,11 +28,16 @@ class ModuleManager:
def _update_cached(self): def _update_cached(self):
for fn, mod in tuple(self.modules.items()): for fn, mod in tuple(self.modules.items()):
if not mod.file_exists(): mod_missing = not mod.file_exists()
print(f"{fn} does not exist... removing cached module") mod_has_changed = mod.has_changed()
del self.modules[fn]
elif mod.has_changed(): if mod_missing or mod_has_changed:
print(f"{fn} has changed... removing cached module for reloading") if mod_missing:
print(f"{fn} does not exist... removing cached module")
elif mod_has_changed:
print(f"{fn} has changed... removing cached module for reloading")
mod.kill()
del self.modules[fn] del self.modules[fn]
@ -49,7 +54,7 @@ class ModuleManager:
mod.load() mod.load()
except ModuleLoadError as e: except ModuleLoadError as e:
print(f"loading {fn} raised", printable_exception(e)) print(f"loading {fn} raised", printable_exception(e))
except MissingRunFunctionError as e: except MissingRunFunctionError:
print(f"missing run function in {fn}") print(f"missing run function in {fn}")
self.modules[fn] = mod self.modules[fn] = mod
@ -61,7 +66,10 @@ class ModuleManager:
print(f"{mod.name} is running already") print(f"{mod.name} is running already")
else: else:
print(f"{mod.name} starting") print(f"{mod.name} starting")
mod.start() try:
mod.start()
except MissingRunFunctionError:
print(f"{mod.name} has no function to run")
@property @property

View File

@ -17,12 +17,17 @@ class Module:
def start(self): def start(self):
if not self.func: if not self.func:
print(f"{self.name} has no function to run") raise MissingRunFunctionError
return
self.task = task = Task(self.func, self.name) self.task = task = Task(self.func, self.name)
task.start() task.start()
return task return task
def kill(self):
if self.task:
self.task.kill()
@property @property
def is_running(self): def is_running(self):
if self.task: if self.task:
@ -48,7 +53,10 @@ class Module:
return self.mtime != self.get_mtime() return self.mtime != self.get_mtime()
def get_mtime(self): def get_mtime(self):
return self.fname.stat().st_mtime try:
return self.fname.stat().st_mtime
except FileNotFoundError:
pass
def __str__(self): def __str__(self):
return f"{self.fname} -> {self.name} ({self.mtime})" return f"{self.fname} -> {self.name} ({self.mtime})"