summaryrefslogtreecommitdiffstats
path: root/Lib/multiprocessing/queues.py
diff options
context:
space:
mode:
authorCharles-François Natali <cf.natali@gmail.com>2014-05-25 13:12:12 (GMT)
committerCharles-François Natali <cf.natali@gmail.com>2014-05-25 13:12:12 (GMT)
commita924fc7abc2d8788a4a9fa2cbef2caa5c5992ebd (patch)
treecb29ab708c0a0c1f8ea51d20cdc7dbdd19a77046 /Lib/multiprocessing/queues.py
parent1691e35953858ae06b5198bf12c72a6cd0e0234b (diff)
downloadcpython-a924fc7abc2d8788a4a9fa2cbef2caa5c5992ebd.zip
cpython-a924fc7abc2d8788a4a9fa2cbef2caa5c5992ebd.tar.gz
cpython-a924fc7abc2d8788a4a9fa2cbef2caa5c5992ebd.tar.bz2
Issue #21565: multiprocessing: use contex-manager protocol for synchronization
primitives.
Diffstat (limited to 'Lib/multiprocessing/queues.py')
-rw-r--r--Lib/multiprocessing/queues.py27
1 files changed, 5 insertions, 22 deletions
diff --git a/Lib/multiprocessing/queues.py b/Lib/multiprocessing/queues.py
index f650771..c07ad40 100644
--- a/Lib/multiprocessing/queues.py
+++ b/Lib/multiprocessing/queues.py
@@ -81,14 +81,11 @@ class Queue(object):
if not self._sem.acquire(block, timeout):
raise Full
- self._notempty.acquire()
- try:
+ with self._notempty:
if self._thread is None:
self._start_thread()
self._buffer.append(obj)
self._notempty.notify()
- finally:
- self._notempty.release()
def get(self, block=True, timeout=None):
if block and timeout is None:
@@ -201,12 +198,9 @@ class Queue(object):
@staticmethod
def _finalize_close(buffer, notempty):
debug('telling queue thread to quit')
- notempty.acquire()
- try:
+ with notempty:
buffer.append(_sentinel)
notempty.notify()
- finally:
- notempty.release()
@staticmethod
def _feed(buffer, notempty, send_bytes, writelock, close, ignore_epipe):
@@ -295,35 +289,24 @@ class JoinableQueue(Queue):
if not self._sem.acquire(block, timeout):
raise Full
- self._notempty.acquire()
- self._cond.acquire()
- try:
+ with self._notempty, self._cond:
if self._thread is None:
self._start_thread()
self._buffer.append(obj)
self._unfinished_tasks.release()
self._notempty.notify()
- finally:
- self._cond.release()
- self._notempty.release()
def task_done(self):
- self._cond.acquire()
- try:
+ with self._cond:
if not self._unfinished_tasks.acquire(False):
raise ValueError('task_done() called too many times')
if self._unfinished_tasks._semlock._is_zero():
self._cond.notify_all()
- finally:
- self._cond.release()
def join(self):
- self._cond.acquire()
- try:
+ with self._cond:
if not self._unfinished_tasks._semlock._is_zero():
self._cond.wait()
- finally:
- self._cond.release()
#
# Simplified Queue type -- really just a locked pipe