Skip to content
Snippets Groups Projects
Commit c258f110 authored by Michael Hanselmann's avatar Michael Hanselmann
Browse files

workerpool: Use itertools.count instead of manual counting


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: default avatarMichael Hanselmann <hansmi@google.com>
Reviewed-by: default avatarBernardo Dal Seno <bdalseno@google.com>
parent b9612abb
No related branches found
No related tags found
No related merge requests found
......@@ -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()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment