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 <Anna@NoWheyCreamery.com>
This commit is contained in:
parent
73019d8eb4
commit
6607e5b0ad
|
@ -65,12 +65,13 @@ class Queue(GObject.GObject):
|
||||||
self.cancel()
|
self.cancel()
|
||||||
|
|
||||||
def push(self, func: typing.Callable, *args,
|
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."""
|
"""Add a task to the Idle Queue."""
|
||||||
if not self.enabled or now:
|
if not self.enabled or now:
|
||||||
return func(*args)
|
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.total += 1
|
||||||
self.__start()
|
self.__start()
|
||||||
|
|
||||||
|
|
|
@ -139,6 +139,17 @@ class TestIdleQueue(unittest.TestCase):
|
||||||
mock_idle_add.assert_not_called()
|
mock_idle_add.assert_not_called()
|
||||||
func.assert_called_with(1)
|
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,
|
def test_push_many_enabled(self, mock_idle_add: unittest.mock.Mock,
|
||||||
mock_source_removed: unittest.mock.Mock):
|
mock_source_removed: unittest.mock.Mock):
|
||||||
"""Test adding several calls to one function at one time."""
|
"""Test adding several calls to one function at one time."""
|
||||||
|
|
Loading…
Reference in New Issue