From c258f1103f13d5c2ec70aa10cb0dbba877d65fec Mon Sep 17 00:00:00 2001
From: Michael Hanselmann <hansmi@google.com>
Date: Fri, 26 Oct 2012 17:38:03 +0200
Subject: [PATCH] workerpool: Use itertools.count instead of manual counting
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Instead of having to explicitely increment the value (β€œβ€¦ += 1”), a call
to next() is enough. These numbers should in no case be re-used (they
are used for ordering tasks). Using β€œitertools.count” is useful here as
it guarantees that a returned number won't be returned another time.
Manual code for this could, over the course of time, gain unintended
bugs.

Signed-off-by: Michael Hanselmann <hansmi@google.com>
Reviewed-by: Bernardo Dal Seno <bdalseno@google.com>
---
 lib/workerpool.py | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)

diff --git a/lib/workerpool.py b/lib/workerpool.py
index fbf562d28..d9e2ae348 100644
--- a/lib/workerpool.py
+++ b/lib/workerpool.py
@@ -26,6 +26,7 @@
 import logging
 import threading
 import heapq
+import itertools
 
 from ganeti import compat
 from ganeti import errors
@@ -252,7 +253,7 @@ class WorkerPool(object):
     self._termworkers = []
 
     # Queued tasks
-    self._counter = 0
+    self._counter = itertools.count()
     self._tasks = []
 
     # Start workers
@@ -279,12 +280,9 @@ class WorkerPool(object):
     assert isinstance(args, (tuple, list)), "Arguments must be a sequence"
     assert isinstance(priority, (int, long)), "Priority must be numeric"
 
-    # This counter is used to ensure elements are processed in their
-    # incoming order. For processing they're sorted by priority and then
-    # counter.
-    self._counter += 1
-
-    heapq.heappush(self._tasks, (priority, self._counter, args))
+    # A counter is used to ensure elements are processed in their incoming
+    # order. For processing they're sorted by priority and then counter.
+    heapq.heappush(self._tasks, (priority, self._counter.next(), args))
 
     # Notify a waiting worker
     self._pool_to_worker.notify()
-- 
GitLab