import logging
import threading
-from ganeti import errors
-from ganeti import utils
-
class BaseWorker(threading.Thread, object):
"""Base worker class for worker pools.
def __init__(self, pool, worker_id):
"""Constructor for BaseWorker thread.
- Args:
- - pool: Parent worker pool
- - worker_id: Identifier for this worker
+ @param pool: the parent worker pool
+ @param worker_id: identifier for this worker
"""
super(BaseWorker, self).__init__()
def RunTask(self, *args):
"""Function called to start a task.
+ This needs to be implemented by child classes.
+
"""
raise NotImplementedError()
This class is thread-safe.
- Tasks are guaranteed to be started in the order in which they're added to the
- pool. Due to the nature of threading, they're not guaranteed to finish in the
- same order.
+ Tasks are guaranteed to be started in the order in which they're
+ added to the pool. Due to the nature of threading, they're not
+ guaranteed to finish in the same order.
"""
def __init__(self, num_workers, worker_class):
"""Constructor for worker pool.
- Args:
- - num_workers: Number of workers to be started (dynamic resizing is not
- yet implemented)
- - worker_class: Class to be instantiated for workers; should derive from
- BaseWorker
+ @param num_workers: number of workers to be started
+ (dynamic resizing is not yet implemented)
+ @param worker_class: the class to be instantiated for workers;
+ should derive from L{BaseWorker}
"""
# Some of these variables are accessed by BaseWorker
def AddTask(self, *args):
"""Adds a task to the queue.
- Args:
- - *args: Arguments passed to BaseWorker.RunTask
+ @param args: arguments passed to L{BaseWorker.RunTask}
"""
self._lock.acquire()
"""
self._lock.acquire()
try:
- return self._ShouldWorkerTerminateUnlocked(self)
+ return self._ShouldWorkerTerminateUnlocked(worker)
finally:
self._lock.release()
self._lock.release()
def _NewWorkerIdUnlocked(self):
+ """Return an identifier for a new worker.
+
+ """
self._last_worker_id += 1
return self._last_worker_id
elif current_count < num_workers:
# Create (num_workers - current_count) new workers
- for i in xrange(num_workers - current_count):
+ for _ in range(num_workers - current_count):
worker = self._worker_class(self, self._NewWorkerIdUnlocked())
self._workers.append(worker)
worker.start()
def Resize(self, num_workers):
"""Changes the number of workers in the pool.
- Args:
- - num_workers: New number of workers
+ @param num_workers: the new number of workers
"""
self._lock.acquire()