Bopytex/bopytex/scheduler.py

58 lines
1.2 KiB
Python
Raw Normal View History

""" Scheduler for action to make """
2022-04-08 20:04:47 +00:00
from logging import exception
class Scheduler:
def __init__(self, actions: list):
self.actions = actions
self._tasks = []
self._done = []
@property
def tasks(self):
return self._tasks
@property
def done(self):
return self._done
def append(self, tasks):
self._tasks += tasks
def dispatch(self, task):
"""Do a task"""
ans = self.actions[task.action](task.deps, task.args)
return ans
2022-04-08 20:04:47 +00:00
def __iter__(self):
return self
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)
self._done.append(ans)
return ans
2022-04-08 20:04:47 +00:00
def run(self):
for i in self:
pass