From 458f8a8269a15a8ed66dfa3c9c1bbc2c5b68bfdc Mon Sep 17 00:00:00 2001 From: Sly_tom_cat Date: Mon, 20 Feb 2017 12:55:40 +0300 Subject: [PATCH 1/3] New Queue.unfinished() method + Doc + Tests --- Doc/library/queue.rst | 11 +++++++++++ Lib/queue.py | 7 +++++++ Lib/test/test_queue.py | 1 + 3 files changed, 19 insertions(+) diff --git a/Doc/library/queue.rst b/Doc/library/queue.rst index bd0fc2d8f3c735..c5e743177589fd 100644 --- a/Doc/library/queue.rst +++ b/Doc/library/queue.rst @@ -190,6 +190,17 @@ Example of how to wait for enqueued tasks to be completed:: for t in threads: t.join() +One more method provides information about progress of tasks completion. + +.. method:: Queue.unfinished() + + Return number of unfinished tasks i.e. number of such tasks which were queued but + consumer threads hadn't yet indicated that task is completed (via call of + :meth:`task_done`). + + To get the correct value from this method you have to call :meth:`task_done` + for each call to :meth:`get` used to fetch a task to indicate that the item was + retrieved and all work on it is complete. .. seealso:: diff --git a/Lib/queue.py b/Lib/queue.py index 572425e844c52e..fc2ce3977712f6 100644 --- a/Lib/queue.py +++ b/Lib/queue.py @@ -82,6 +82,13 @@ def join(self): while self.unfinished_tasks: self.all_tasks_done.wait() + def unfinished(self): + '''Return number of unfinished tasks i.e. number of such tasks which + were queued but consumer threads hadn't yet indicated that task is + completed (via call of method task_done()). + ''' + return self.unfinished_tasks + def qsize(self): '''Return the approximate size of the queue (not reliable!).''' with self.mutex: diff --git a/Lib/test/test_queue.py b/Lib/test/test_queue.py index 4ccaa39adff69f..2ef734d80fc536 100644 --- a/Lib/test/test_queue.py +++ b/Lib/test/test_queue.py @@ -99,6 +99,7 @@ def simple_queue_test(self, q): q.put(111) q.put(333) q.put(222) + self.assertEqual(q.unfinished(), 3) target_order = dict(Queue = [111, 333, 222], LifoQueue = [222, 333, 111], PriorityQueue = [111, 222, 333]) From 485d8311568999a0f91f13d0f752073018c86b69 Mon Sep 17 00:00:00 2001 From: Sly_tom_cat Date: Mon, 20 Feb 2017 13:30:52 +0300 Subject: [PATCH 2/3] correct tests --- Lib/test/test_queue.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_queue.py b/Lib/test/test_queue.py index 2ef734d80fc536..52f8b1b76c2cdb 100644 --- a/Lib/test/test_queue.py +++ b/Lib/test/test_queue.py @@ -99,7 +99,6 @@ def simple_queue_test(self, q): q.put(111) q.put(333) q.put(222) - self.assertEqual(q.unfinished(), 3) target_order = dict(Queue = [111, 333, 222], LifoQueue = [222, 333, 111], PriorityQueue = [111, 222, 333]) @@ -194,6 +193,14 @@ def test_queue_join(self): else: self.fail("Did not detect task count going negative") + def test_queue_unfinished(self): + q = self.type2test() + q.put(1) + q.put(2) + self.assertEqual(q.unfinished(), 2) + q.task_done() + self.assertEqual(q.unfinished(), 1) + def test_simple_queue(self): # Do it a couple of times on the same queue. # Done twice to make sure works with same instance reused. From 67b64667c6fbf3033d7d4d7de879592f38843d05 Mon Sep 17 00:00:00 2001 From: Sly_tom_cat Date: Mon, 20 Feb 2017 13:32:48 +0300 Subject: [PATCH 3/3] Fixed tests --- Lib/test/test_queue.py | 1 + 1 file changed, 1 insertion(+) diff --git a/Lib/test/test_queue.py b/Lib/test/test_queue.py index 52f8b1b76c2cdb..377ebeb188709f 100644 --- a/Lib/test/test_queue.py +++ b/Lib/test/test_queue.py @@ -198,6 +198,7 @@ def test_queue_unfinished(self): q.put(1) q.put(2) self.assertEqual(q.unfinished(), 2) + q.get() q.task_done() self.assertEqual(q.unfinished(), 1)