FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

bpo-35477: multiprocessing.Pool.__enter__() fails if called twice by vstinner · Pull Request #11134 · python/cpython · GitHub

/ cpython Public
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
17 changes: 9 additions & 8 deletions Lib/multiprocessing/pool.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,10 @@ def _setup_queues(self):
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)`.
Expand Down Expand Up @@ -306,8 +310,7 @@ def imap(self, func, iterable, chunksize=1):
'''
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(
Expand Down Expand Up @@ -336,8 +339,7 @@ def imap_unordered(self, func, iterable, chunksize=1):
'''
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(
Expand Down Expand Up @@ -366,8 +368,7 @@ def apply_async(self, func, args=(), kwds={}, callback=None,
'''
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
Expand All @@ -385,8 +386,7 @@ def _map_async(self, func, iterable, mapper, chunksize=None, callback=None,
'''
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)

Expand Down Expand Up @@ -625,6 +625,7 @@ def _terminate_pool(cls, taskqueue, inqueue, outqueue, pool,
p.join()

def __enter__(self):
self._check_running()
return self

def __exit__(self, exc_type, exc_val, exc_tb):
Expand Down
16 changes: 16 additions & 0 deletions Lib/test/_test_multiprocessing.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2561,6 +2561,22 @@ def test_release_task_refs(self):
# 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")
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:meth:`multiprocessing.Pool.__enter__` now fails if the pool is not running:
``with pool:`` fails if used more than once.

Back | FazBrowse Home | New Git URL