Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 80a5be1

Browse files
committed
Issue #20980: Stop wrapping exception when using ThreadPool.
1 parent a40675a commit 80a5be1

4 files changed

Lines changed: 30 additions & 6 deletions

File tree

Lib/multiprocessing/managers.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1077,17 +1077,22 @@ def __imul__(self, value):
10771077
))
10781078

10791079

1080-
PoolProxy = MakeProxyType('PoolProxy', (
1080+
BasePoolProxy = MakeProxyType('PoolProxy', (
10811081
'apply', 'apply_async', 'close', 'imap', 'imap_unordered', 'join',
1082-
'map', 'map_async', 'starmap', 'starmap_async', 'terminate'
1082+
'map', 'map_async', 'starmap', 'starmap_async', 'terminate',
10831083
))
1084-
PoolProxy._method_to_typeid_ = {
1084+
BasePoolProxy._method_to_typeid_ = {
10851085
'apply_async': 'AsyncResult',
10861086
'map_async': 'AsyncResult',
10871087
'starmap_async': 'AsyncResult',
10881088
'imap': 'Iterator',
10891089
'imap_unordered': 'Iterator'
10901090
}
1091+
class PoolProxy(BasePoolProxy):
1092+
def __enter__(self):
1093+
return self
1094+
def __exit__(self, exc_type, exc_val, exc_tb):
1095+
self.terminate()
10911096

10921097
#
10931098
# Definition of SyncManager

Lib/multiprocessing/pool.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ def __repr__(self):
9090
return "<MaybeEncodingError: %s>" % str(self)
9191

9292

93-
def worker(inqueue, outqueue, initializer=None, initargs=(), maxtasks=None):
93+
def worker(inqueue, outqueue, initializer=None, initargs=(), maxtasks=None,
94+
wrap_exception=False):
9495
assert maxtasks is None or (type(maxtasks) == int and maxtasks > 0)
9596
put = outqueue.put
9697
get = inqueue.get
@@ -117,7 +118,8 @@ def worker(inqueue, outqueue, initializer=None, initargs=(), maxtasks=None):
117118
try:
118119
result = (True, func(*args, **kwds))
119120
except Exception as e:
120-
e = ExceptionWithTraceback(e, e.__traceback__)
121+
if wrap_exception:
122+
e = ExceptionWithTraceback(e, e.__traceback__)
121123
result = (False, e)
122124
try:
123125
put((job, i, result))
@@ -137,6 +139,8 @@ class Pool(object):
137139
'''
138140
Class which supports an async version of applying functions to arguments.
139141
'''
142+
_wrap_exception = True
143+
140144
def Process(self, *args, **kwds):
141145
return self._ctx.Process(*args, **kwds)
142146

@@ -220,7 +224,8 @@ def _repopulate_pool(self):
220224
w = self.Process(target=worker,
221225
args=(self._inqueue, self._outqueue,
222226
self._initializer,
223-
self._initargs, self._maxtasksperchild)
227+
self._initargs, self._maxtasksperchild,
228+
self._wrap_exception)
224229
)
225230
self._pool.append(w)
226231
w.name = w.name.replace('Process', 'PoolWorker')
@@ -736,6 +741,7 @@ def _set(self, i, obj):
736741
#
737742

738743
class ThreadPool(Pool):
744+
_wrap_exception = False
739745

740746
@staticmethod
741747
def Process(*args, **kwds):

Lib/test/_test_multiprocessing.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1810,6 +1810,17 @@ def test_traceback(self):
18101810
self.assertIn('raise RuntimeError(123) # some comment',
18111811
f1.getvalue())
18121812

1813+
@classmethod
1814+
def _test_wrapped_exception(cls):
1815+
raise RuntimeError('foo')
1816+
1817+
def test_wrapped_exception(self):
1818+
# Issue #20980: Should not wrap exception when using thread pool
1819+
with self.Pool(1) as p:
1820+
with self.assertRaises(RuntimeError):
1821+
p.apply(self._test_wrapped_exception)
1822+
1823+
18131824
def raising():
18141825
raise KeyError("key")
18151826

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ Core and Builtins
2121
Library
2222
-------
2323

24+
- Issue #20980: Stop wrapping exception when using ThreadPool.
25+
2426
- Issue #20990: Fix issues found by pyflakes for multiprocessing.
2527

2628
- Issue #21015: SSL contexts will now automatically select an elliptic

0 commit comments

Comments
 (0)