Permalink
Browse files

Use with statements to acquire queue locks.

  • Loading branch information...
J. Victor Martins
J. Victor Martins committed Mar 25, 2011
1 parent 8f5dca1 commit fef493e1055007922b3262ba2da20f0d265439e3
Showing with 10 additions and 14 deletions.
  1. +10 −14 mdvpkg/worker.py
View
@@ -153,9 +153,8 @@ def __init__(self, backend_path):
def push(self, task):
""" Add a task to the task queue. """
- self._queue_lock.acquire()
- self._queue[task.path] = task
- self._queue_lock.release()
+ with self._queue_lock:
+ self._queue[task.path] = task
self._new_task.set()
def inactive(self, idle_timeout):
@@ -177,30 +176,27 @@ def cancel(self, task):
else:
# Not running the task, so we remove it from the queue.
# It's an error if the task was not queued ...
- self._queue_lock.acquire()
- t = self._queue.pop(task.path, None)
- if not t:
- log.error('Cancelling not queued task')
- if t != task:
- log.error('Cancelling a task with different path')
- self._queue_lock.release()
+ with self._queue_lock:
+ t = self._queue.pop(task.path, None)
+ if not t:
+ log.error('Cancelling not queued task')
+ if t != task:
+ log.error('Cancelling a task with different path')
def _work_loop(self):
""" Worker's thread activity method. """
log.info("Thread initialized")
self.__work = True
while self.__work:
try:
- self._queue_lock.acquire()
- (path, self._task) = self._queue.popitem(last=False)
+ with self._queue_lock:
+ (path, self._task) = self._queue.popitem(last=False)
except KeyError:
- self._queue_lock.release()
if not self._new_task.wait(WAIT_TASK_TIMEOUT) \
and self._backend.running():
log.info('No tasks available, Killing backend')
self._backend.kill()
else:
- self._queue_lock.release()
self._new_task.clear()
try:
self._last_action_timestamp = time.time()

0 comments on commit fef493e

Please sign in to comment.