X-Git-Url: https://code.grnet.gr/git/ganeti-local/blobdiff_plain/7260cfbe90e7bf0a30b296a8196618c8558d08b9..4d32febaf5b3a5dbee3100883f225ee82c2e2ace:/lib/workerpool.py diff --git a/lib/workerpool.py b/lib/workerpool.py index 2d3937a..1ec89af 100644 --- a/lib/workerpool.py +++ b/lib/workerpool.py @@ -42,9 +42,8 @@ class BaseWorker(threading.Thread, object): @param worker_id: identifier for this worker """ - super(BaseWorker, self).__init__() + super(BaseWorker, self).__init__(name=worker_id) self.pool = pool - self.worker_id = worker_id self._current_task = None def ShouldTerminate(self): @@ -89,12 +88,12 @@ class BaseWorker(threading.Thread, object): # We only wait if there's no task for us. if not pool._tasks: - logging.debug("Worker %s: waiting for tasks", self.worker_id) + logging.debug("Waiting for tasks") # wait() releases the lock and sleeps until notified pool._pool_to_worker.wait() - logging.debug("Worker %s: notified while waiting", self.worker_id) + logging.debug("Notified while waiting") # Were we woken up in order to terminate? if pool._ShouldWorkerTerminateUnlocked(self): @@ -114,14 +113,11 @@ class BaseWorker(threading.Thread, object): # Run the actual task try: - logging.debug("Worker %s: starting task %r", - self.worker_id, self._current_task) + logging.debug("Starting task %r", self._current_task) self.RunTask(*self._current_task) - logging.debug("Worker %s: done with task %r", - self.worker_id, self._current_task) + logging.debug("Done with task %r", self._current_task) except: # pylint: disable-msg=W0702 - logging.error("Worker %s: Caught unhandled exception", - self.worker_id, exc_info=True) + logging.exception("Caught unhandled exception") finally: # Notify pool pool._lock.acquire() @@ -132,7 +128,7 @@ class BaseWorker(threading.Thread, object): finally: pool._lock.release() - logging.debug("Worker %s: terminates", self.worker_id) + logging.debug("Terminates") def RunTask(self, *args): """Function called to start a task. @@ -153,7 +149,7 @@ class WorkerPool(object): guaranteed to finish in the same order. """ - def __init__(self, num_workers, worker_class): + def __init__(self, name, num_workers, worker_class): """Constructor for worker pool. @param num_workers: number of workers to be started @@ -168,6 +164,7 @@ class WorkerPool(object): self._pool_to_worker = threading.Condition(self._lock) self._worker_to_pool = threading.Condition(self._lock) self._worker_class = worker_class + self._name = name self._last_worker_id = 0 self._workers = [] self._quiescing = False @@ -253,7 +250,8 @@ class WorkerPool(object): """ self._last_worker_id += 1 - return self._last_worker_id + + return "%s%d" % (self._name, self._last_worker_id) def _ResizeUnlocked(self, num_workers): """Changes the number of workers.