From e0377a3e92663f6ab9a8c6ffd405031db6ed0096 Mon Sep 17 00:00:00 2001 From: Bertrand Benjamin Date: Fri, 8 Apr 2022 21:49:18 +0200 Subject: [PATCH] Feat: scheduler handle dependencies (basic) --- bopytex/scheduler.py | 9 ++++++++- test/test_scheduler.py | 25 +++++++++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) diff --git a/bopytex/scheduler.py b/bopytex/scheduler.py index db141c0..db00b12 100644 --- a/bopytex/scheduler.py +++ b/bopytex/scheduler.py @@ -19,12 +19,19 @@ class Scheduler: self._tasks += tasks def dispatch(self, task): - """ Do a task """ + """Do a task""" ans = self.actions[task.action](task.deps, task.args) return ans def __next__(self): + undoable = [] + task = self._tasks.pop(0) + while not all([d in self.done for d in task.deps]): + undoable.append(task) + task = self._tasks.pop(0) + + self.append(undoable) ans = self.dispatch(task) self._done.append(ans) return ans diff --git a/test/test_scheduler.py b/test/test_scheduler.py index 183a878..a79c764 100644 --- a/test/test_scheduler.py +++ b/test/test_scheduler.py @@ -59,3 +59,28 @@ def test_schedule_multiple_tasks(): assert result == "three done" assert scheduler.tasks == [] assert scheduler.done == ["one done", "two done", "three done"] + + +def test_schedule_multiple_tasks_with_dependencies(): + actions = {"DO": lambda deps, args: f"{args['task']} done"} + scheduler = Scheduler(actions) + t1 = Task(action="DO", args={"task": "one"}, deps=["three done"]) + t2 = Task(action="DO", args={"task": "two"}, deps=["one done"]) + t3 = Task(action="DO", args={"task": "three"}, deps=[]) + scheduler.append([t1, t2, t3]) + + result = scheduler.__next__() + assert result == "three done" + assert scheduler.tasks == [t1, t2] + assert scheduler.done == ["three done"] + + result = scheduler.__next__() + assert result == "one done" + assert scheduler.tasks == [t2] + assert scheduler.done == ["three done", "one done"] + + result = scheduler.__next__() + assert result == "two done" + assert scheduler.tasks == [] + assert scheduler.done == ["three done", "one done", "two done"] +