diff options
author | Victor Stinner <vstinner@redhat.com> | 2018-12-13 01:15:30 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-12-13 01:15:30 (GMT) |
commit | 08c2ba0717089662132af69bf5948d82277a8a69 (patch) | |
tree | 65536d44e81385773a0a4eaf4b047b6b3ab3575a /Lib | |
parent | 502fe19b10f66235fcf8f13fc1c0308190845def (diff) | |
download | cpython-08c2ba0717089662132af69bf5948d82277a8a69.zip cpython-08c2ba0717089662132af69bf5948d82277a8a69.tar.gz cpython-08c2ba0717089662132af69bf5948d82277a8a69.tar.bz2 |
bpo-35477: multiprocessing.Pool.__enter__() fails if called twice (GH-11134)
multiprocessing.Pool.__enter__() now fails if the pool is not
running: "with pool:" fails if used more than once.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/multiprocessing/pool.py | 17 | ||||
-rw-r--r-- | Lib/test/_test_multiprocessing.py | 16 |
2 files changed, 25 insertions, 8 deletions
diff --git a/Lib/multiprocessing/pool.py b/Lib/multiprocessing/pool.py index 2b3cc59..c077541 100644 --- a/Lib/multiprocessing/pool.py +++ b/Lib/multiprocessing/pool.py @@ -261,6 +261,10 @@ class Pool(object): self._quick_put = self._inqueue._writer.send self._quick_get = self._outqueue._reader.recv + def _check_running(self): + if self._state != RUN: + raise ValueError("Pool not running") + def apply(self, func, args=(), kwds={}): ''' Equivalent of `func(*args, **kwds)`. @@ -306,8 +310,7 @@ class Pool(object): ''' Equivalent of `map()` -- can be MUCH slower than `Pool.map()`. ''' - if self._state != RUN: - raise ValueError("Pool not running") + self._check_running() if chunksize == 1: result = IMapIterator(self._cache) self._taskqueue.put( @@ -336,8 +339,7 @@ class Pool(object): ''' Like `imap()` method but ordering of results is arbitrary. ''' - if self._state != RUN: - raise ValueError("Pool not running") + self._check_running() if chunksize == 1: result = IMapUnorderedIterator(self._cache) self._taskqueue.put( @@ -366,8 +368,7 @@ class Pool(object): ''' Asynchronous version of `apply()` method. ''' - if self._state != RUN: - raise ValueError("Pool not running") + self._check_running() result = ApplyResult(self._cache, callback, error_callback) self._taskqueue.put(([(result._job, 0, func, args, kwds)], None)) return result @@ -385,8 +386,7 @@ class Pool(object): ''' Helper function to implement map, starmap and their async counterparts. ''' - if self._state != RUN: - raise ValueError("Pool not running") + self._check_running() if not hasattr(iterable, '__len__'): iterable = list(iterable) @@ -625,6 +625,7 @@ class Pool(object): p.join() def __enter__(self): + self._check_running() return self def __exit__(self, exc_type, exc_val, exc_tb): diff --git a/Lib/test/_test_multiprocessing.py b/Lib/test/_test_multiprocessing.py index 0b0fe7c..a2dc53c 100644 --- a/Lib/test/_test_multiprocessing.py +++ b/Lib/test/_test_multiprocessing.py @@ -2561,6 +2561,22 @@ class _TestPool(BaseTestCase): # they were released too. self.assertEqual(CountedObject.n_instances, 0) + def test_enter(self): + if self.TYPE == 'manager': + self.skipTest("test not applicable to manager") + + pool = self.Pool(1) + with pool: + pass + # call pool.terminate() + # pool is no longer running + + with self.assertRaises(ValueError): + # bpo-35477: pool.__enter__() fails if the pool is not running + with pool: + pass + pool.join() + def raising(): raise KeyError("key") |