diff options
Diffstat (limited to 'cxmanage_api/tasks.py')
-rw-r--r-- | cxmanage_api/tasks.py | 24 |
1 files changed, 16 insertions, 8 deletions
diff --git a/cxmanage_api/tasks.py b/cxmanage_api/tasks.py index 6b5cfde..d241c30 100644 --- a/cxmanage_api/tasks.py +++ b/cxmanage_api/tasks.py @@ -1,4 +1,7 @@ -# Copyright (c) 2012, Calxeda Inc. +"""Calxeda: tasks.py""" + + +# Copyright (c) 2012-2013, Calxeda Inc. # # All rights reserved. # @@ -43,7 +46,7 @@ class Task(object): :type args: list """ - def __init__(self, method, *args): + def __init__(self, method, *args, **kwargs): """Default constructor for the Task class.""" self.status = "Queued" self.result = None @@ -51,6 +54,7 @@ class Task(object): self._method = method self._args = args + self._kwargs = kwargs self._finished = Event() def join(self): @@ -70,10 +74,11 @@ class Task(object): """Execute this task. Should only be called by TaskWorker.""" self.status = "In Progress" try: - self.result = self._method(*self._args) + self.result = self._method(*self._args, **self._kwargs) self.status = "Completed" - except Exception as e: - self.error = e + # pylint: disable=W0703 + except Exception as err: + self.error = err self.status = "Failed" self._finished.set() @@ -96,7 +101,7 @@ class TaskQueue(object): self._queue = deque() self._workers = 0 - def put(self, method, *args): + def put(self, method, *args, **kwargs): """Add a task to the task queue, and spawn a worker if we're not full. :param method: Named method to run. @@ -110,7 +115,7 @@ class TaskQueue(object): """ self._lock.acquire() - task = Task(method, *args) + task = Task(method, *args, **kwargs) self._queue.append(task) if self._workers < self.threads: @@ -166,8 +171,11 @@ class TaskWorker(Thread): while True: sleep(self._delay) task = self._task_queue.get() + # pylint: disable=W0212 task._run() - except: + # pylint: disable=W0703 + except Exception: + # pylint: disable=W0212 self._task_queue._remove_worker() DEFAULT_TASK_QUEUE = TaskQueue() |