Bopytex/bopytex/scheduler.py

83 lines
1.8 KiB
Python
Raw Normal View History

""" Scheduler for action to make """
2022-04-09 04:58:44 +00:00
from bopytex.tasks import Task
2022-04-09 04:56:57 +00:00
class Scheduler:
2022-04-09 14:16:39 +00:00
def __init__(self, actions: dict, done: list[str] = None):
self.actions = actions
2022-04-09 04:56:57 +00:00
if done is None:
self._done = []
else:
self._done = done
self._tasks = []
@property
def tasks(self):
return self._tasks
2022-04-09 15:08:05 +00:00
@property
def doable_tasks(self):
return [
task
for task in self.tasks
if not task.deps or all([d in self.done for d in task.deps])
]
2022-04-09 04:56:57 +00:00
@property
def all_deps(self):
return {d for task in self.tasks for d in task.deps}
@property
def all_output(self):
return {task.output for task in self.tasks}
@property
def done(self):
return self._done
2022-04-09 04:56:57 +00:00
def append(self, tasks: list[Task]):
self._tasks += tasks
def dispatch(self, task):
"""Do a task"""
2022-04-09 04:32:56 +00:00
ans = self.actions[task.action](task.deps, task.args, task.output)
return ans
2022-04-08 20:04:47 +00:00
def __iter__(self):
return self
2022-04-09 04:13:40 +00:00
def __next__(self):
2022-04-08 20:04:47 +00:00
return self.next()
def next(self):
undoable = []
2022-04-08 20:04:47 +00:00
try:
task = self._tasks.pop(0)
except IndexError:
raise StopIteration
while not all([d in self.done for d in task.deps]):
undoable.append(task)
2022-04-08 20:04:47 +00:00
try:
task = self._tasks.pop(0)
except IndexError:
self.append(undoable)
raise StopIteration
self.append(undoable)
2022-04-08 19:29:35 +00:00
ans = self.dispatch(task)
2022-04-09 04:32:56 +00:00
self._done.append(task.output)
2022-04-08 19:29:35 +00:00
return ans
2022-04-08 20:04:47 +00:00
def run(self):
2022-04-09 04:13:40 +00:00
for _ in self:
2022-04-08 20:04:47 +00:00
pass
2022-04-09 04:56:57 +00:00
def is_finishable(self):
return self.all_deps.issubset(self.all_output)