added running the script to Module, all are ran by ModuleManager

This commit is contained in:
2021-02-08 10:06:51 +00:00
parent 338a14d3cb
commit 645140add7
3 changed files with 41 additions and 1 deletions

View File

@ -23,6 +23,7 @@ class ModuleManager:
def update(self):
self._update_cached()
self._update_new()
self._run_all()
def _update_cached(self):
@ -54,6 +55,15 @@ class ModuleManager:
self.modules[fn] = mod
def _run_all(self):
for mod in self.modules.values():
if mod.is_running:
print(f"{mod.name} is running already")
else:
print(f"{mod.name} starting")
mod.start()
@property
def fnames(self):
fns = self.folder.glob("*.py")

View File

@ -1,6 +1,7 @@
from pathlib import Path
from importing import load_module
from task import Task
from utils import printable_exception
@ -11,6 +12,21 @@ class Module:
self.fname = fname
self.name = fname.stem.replace("-", "_")
self.mtime = self.get_mtime()
self.mod = self.func = self.task = None
def start(self):
if not self.func:
print(f"{self.name} has no function to run")
return
self.task = task = Task(self.func, self.name)
task.start()
return task
@property
def is_running(self):
if self.task:
return self.task.is_running
def load(self):
@ -20,7 +36,7 @@ class Module:
raise ModuleLoadError(printable_exception(e)) from e
try:
self.run = mod.run
self.func = mod.run #TODO this function name? is "main" better?
except AttributeError as e:
raise MissingRunFunctionError from e

14
modman/task.py Normal file
View File

@ -0,0 +1,14 @@
from threading import Thread
class Task(Thread):
def __init__(self, target, name, **kwargs):
super().__init__(group=None, target=target, name=name, **kwargs)
@property
def is_running(self):
return self.is_alive()