Issue #15064: Implement context manager protocol for multiprocessing types
diff --git a/Lib/multiprocessing/connection.py b/Lib/multiprocessing/connection.py
index 56f375d..e5694e3 100644
--- a/Lib/multiprocessing/connection.py
+++ b/Lib/multiprocessing/connection.py
@@ -257,6 +257,12 @@
self._check_readable()
return self._poll(timeout)
+ def __enter__(self):
+ return self
+
+ def __exit__(self, exc_type, exc_value, exc_tb):
+ self.close()
+
if _winapi:
@@ -436,6 +442,8 @@
Returns a `Connection` object.
'''
+ if self._listener is None:
+ raise IOError('listener is closed')
c = self._listener.accept()
if self._authkey:
deliver_challenge(c, self._authkey)
@@ -446,11 +454,19 @@
'''
Close the bound socket or named pipe of `self`.
'''
- return self._listener.close()
+ if self._listener is not None:
+ self._listener.close()
+ self._listener = None
address = property(lambda self: self._listener._address)
last_accepted = property(lambda self: self._listener._last_accepted)
+ def __enter__(self):
+ return self
+
+ def __exit__(self, exc_type, exc_value, exc_tb):
+ self.close()
+
def Client(address, family=None, authkey=None):
'''
diff --git a/Lib/multiprocessing/dummy/connection.py b/Lib/multiprocessing/dummy/connection.py
index af10579..874ec8e 100644
--- a/Lib/multiprocessing/dummy/connection.py
+++ b/Lib/multiprocessing/dummy/connection.py
@@ -53,6 +53,12 @@
address = property(lambda self: self._backlog_queue)
+ def __enter__(self):
+ return self
+
+ def __exit__(self, exc_type, exc_value, exc_tb):
+ self.close()
+
def Client(address):
_in, _out = Queue(), Queue()
@@ -85,3 +91,9 @@
def close(self):
pass
+
+ def __enter__(self):
+ return self
+
+ def __exit__(self, exc_type, exc_value, exc_tb):
+ self.close()
diff --git a/Lib/multiprocessing/pool.py b/Lib/multiprocessing/pool.py
index 59e547a..9e07e32 100644
--- a/Lib/multiprocessing/pool.py
+++ b/Lib/multiprocessing/pool.py
@@ -522,6 +522,12 @@
debug('cleaning up worker %d' % p.pid)
p.join()
+ def __enter__(self):
+ return self
+
+ def __exit__(self, exc_type, exc_val, exc_tb):
+ self.terminate()
+
#
# Class whose instances are returned by `Pool.apply_async()`
#
diff --git a/Lib/test/test_multiprocessing.py b/Lib/test/test_multiprocessing.py
index 08f87cd..017e6b4 100644
--- a/Lib/test/test_multiprocessing.py
+++ b/Lib/test/test_multiprocessing.py
@@ -1719,6 +1719,15 @@
p.close()
p.join()
+ def test_context(self):
+ if self.TYPE == 'processes':
+ L = list(range(10))
+ expected = [sqr(i) for i in L]
+ with multiprocessing.Pool(2) as p:
+ r = p.map_async(sqr, L)
+ self.assertEqual(r.get(), expected)
+ self.assertRaises(AssertionError, p.map_async, sqr, L)
+
def raising():
raise KeyError("key")
@@ -2266,6 +2275,22 @@
self.assertRaises(RuntimeError, reduction.recv_handle, conn)
p.join()
+ def test_context(self):
+ a, b = self.Pipe()
+
+ with a, b:
+ a.send(1729)
+ self.assertEqual(b.recv(), 1729)
+ if self.TYPE == 'processes':
+ self.assertFalse(a.closed)
+ self.assertFalse(b.closed)
+
+ if self.TYPE == 'processes':
+ self.assertTrue(a.closed)
+ self.assertTrue(b.closed)
+ self.assertRaises(IOError, a.recv)
+ self.assertRaises(IOError, b.recv)
+
class _TestListener(BaseTestCase):
ALLOWED_TYPES = ('processes',)
@@ -2277,6 +2302,16 @@
self.assertRaises(OSError, self.connection.Listener,
l.address, family)
+ def test_context(self):
+ with self.connection.Listener() as l:
+ with self.connection.Client(l.address) as c:
+ with l.accept() as d:
+ c.send(1729)
+ self.assertEqual(d.recv(), 1729)
+
+ if self.TYPE == 'processes':
+ self.assertRaises(IOError, l.accept)
+
class _TestListenerClient(BaseTestCase):
ALLOWED_TYPES = ('processes', 'threads')