From 6607e5b0ad1c50f3e01f4c9dd388993e77cf8a4e Mon Sep 17 00:00:00 2001 From: Anna Schumaker Date: Sun, 25 Jun 2023 09:32:00 -0400 Subject: [PATCH] db: Give the Idle Queue a way to handle high priority tasks If we have a high priority task, then we want to push it to the front of the idle queue so it runs as soon as possible. Signed-off-by: Anna Schumaker --- emmental/db/idle.py | 5 +++-- tests/db/test_idle.py | 11 +++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/emmental/db/idle.py b/emmental/db/idle.py index 78e350c..0bfaceb 100644 --- a/emmental/db/idle.py +++ b/emmental/db/idle.py @@ -65,12 +65,13 @@ class Queue(GObject.GObject): self.cancel() def push(self, func: typing.Callable, *args, - now: bool = False) -> bool | None: + now: bool = False, first: bool = False) -> bool | None: """Add a task to the Idle Queue.""" if not self.enabled or now: return func(*args) - self._tasks.append((func, *args)) + pos = 0 if first else len(self._tasks) + self._tasks.insert(pos, (func, *args)) self.total += 1 self.__start() diff --git a/tests/db/test_idle.py b/tests/db/test_idle.py index 6a73486..7c378ae 100644 --- a/tests/db/test_idle.py +++ b/tests/db/test_idle.py @@ -139,6 +139,17 @@ class TestIdleQueue(unittest.TestCase): mock_idle_add.assert_not_called() func.assert_called_with(1) + def test_push_first(self, mock_idle_add: unittest.mock.Mock, + mock_source_removed: unittest.mock.Mock): + """Test pushing an idle task with first=True.""" + self.queue.push(1) + self.queue.push(0, first=True) + self.assertListEqual(self.queue._tasks, [(0,), (1,)]) + self.queue.push(2, first=False) + self.assertListEqual(self.queue._tasks, [(0,), (1,), (2,)]) + self.queue.push(3) + self.assertListEqual(self.queue._tasks, [(0,), (1,), (2,), (3,)]) + def test_push_many_enabled(self, mock_idle_add: unittest.mock.Mock, mock_source_removed: unittest.mock.Mock): """Test adding several calls to one function at one time."""