[Python-checkins] cpython (3.4): Issue #20980: Stop wrapping exception when using ThreadPool.

richard.oudkerk python-checkins at python.org
Sun Mar 23 13:33:18 CET 2014


http://hg.python.org/cpython/rev/df6a6951b2c9
changeset: 89925:df6a6951b2c9
branch: 3.4
parent: 89923:619331c67638
user: Richard Oudkerk <shibturn at gmail.com>
date: Sun Mar 23 12:30:54 2014 +0000
summary:
 Issue #20980: Stop wrapping exception when using ThreadPool.
files:
 Lib/multiprocessing/managers.py | 11 ++++++++---
 Lib/multiprocessing/pool.py | 12 +++++++++---
 Lib/test/_test_multiprocessing.py | 11 +++++++++++
 Misc/NEWS | 2 ++
 4 files changed, 30 insertions(+), 6 deletions(-)
diff --git a/Lib/multiprocessing/managers.py b/Lib/multiprocessing/managers.py
--- a/Lib/multiprocessing/managers.py
+++ b/Lib/multiprocessing/managers.py
@@ -1077,17 +1077,22 @@
 ))
 
 
-PoolProxy = MakeProxyType('PoolProxy', (
+BasePoolProxy = MakeProxyType('PoolProxy', (
 'apply', 'apply_async', 'close', 'imap', 'imap_unordered', 'join',
- 'map', 'map_async', 'starmap', 'starmap_async', 'terminate'
+ 'map', 'map_async', 'starmap', 'starmap_async', 'terminate',
 ))
-PoolProxy._method_to_typeid_ = {
+BasePoolProxy._method_to_typeid_ = {
 'apply_async': 'AsyncResult',
 'map_async': 'AsyncResult',
 'starmap_async': 'AsyncResult',
 'imap': 'Iterator',
 'imap_unordered': 'Iterator'
 }
+class PoolProxy(BasePoolProxy):
+ def __enter__(self):
+ return self
+ def __exit__(self, exc_type, exc_val, exc_tb):
+ self.terminate()
 
 #
 # Definition of SyncManager
diff --git a/Lib/multiprocessing/pool.py b/Lib/multiprocessing/pool.py
--- a/Lib/multiprocessing/pool.py
+++ b/Lib/multiprocessing/pool.py
@@ -90,7 +90,8 @@
 return "<MaybeEncodingError: %s>" % str(self)
 
 
-def worker(inqueue, outqueue, initializer=None, initargs=(), maxtasks=None):
+def worker(inqueue, outqueue, initializer=None, initargs=(), maxtasks=None,
+ wrap_exception=False):
 assert maxtasks is None or (type(maxtasks) == int and maxtasks > 0)
 put = outqueue.put
 get = inqueue.get
@@ -117,7 +118,8 @@
 try:
 result = (True, func(*args, **kwds))
 except Exception as e:
- e = ExceptionWithTraceback(e, e.__traceback__)
+ if wrap_exception:
+ e = ExceptionWithTraceback(e, e.__traceback__)
 result = (False, e)
 try:
 put((job, i, result))
@@ -137,6 +139,8 @@
 '''
 Class which supports an async version of applying functions to arguments.
 '''
+ _wrap_exception = True
+
 def Process(self, *args, **kwds):
 return self._ctx.Process(*args, **kwds)
 
@@ -220,7 +224,8 @@
 w = self.Process(target=worker,
 args=(self._inqueue, self._outqueue,
 self._initializer,
- self._initargs, self._maxtasksperchild)
+ self._initargs, self._maxtasksperchild,
+ self._wrap_exception)
 )
 self._pool.append(w)
 w.name = w.name.replace('Process', 'PoolWorker')
@@ -736,6 +741,7 @@
 #
 
 class ThreadPool(Pool):
+ _wrap_exception = False
 
 @staticmethod
 def Process(*args, **kwds):
diff --git a/Lib/test/_test_multiprocessing.py b/Lib/test/_test_multiprocessing.py
--- a/Lib/test/_test_multiprocessing.py
+++ b/Lib/test/_test_multiprocessing.py
@@ -1810,6 +1810,17 @@
 self.assertIn('raise RuntimeError(123) # some comment',
 f1.getvalue())
 
+ @classmethod
+ def _test_wrapped_exception(cls):
+ raise RuntimeError('foo')
+
+ def test_wrapped_exception(self):
+ # Issue #20980: Should not wrap exception when using thread pool
+ with self.Pool(1) as p:
+ with self.assertRaises(RuntimeError):
+ p.apply(self._test_wrapped_exception)
+
+
 def raising():
 raise KeyError("key")
 
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -21,6 +21,8 @@
 Library
 -------
 
+- Issue #20980: Stop wrapping exception when using ThreadPool.
+
 - Issue #20990: Fix issues found by pyflakes for multiprocessing.
 
 - Issue #21015: SSL contexts will now automatically select an elliptic
-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /