]> granicus.if.org Git - python/commitdiff
Issue #23051: multiprocessing.Pool methods imap() and imap_unordered() now
authorSerhiy Storchaka <storchaka@gmail.com>
Fri, 13 Mar 2015 06:25:26 +0000 (08:25 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Fri, 13 Mar 2015 06:25:26 +0000 (08:25 +0200)
handle exceptions raised by an iterator.  Patch by Alon Diamant and Davin
Potts.

Lib/multiprocessing/pool.py
Lib/test/_test_multiprocessing.py
Misc/ACKS
Misc/NEWS

index 8832a5ceb25f84d00d847c763b53bd63cb8c6b04..db6e3e179f4b8bb8ad8f6a4c7032da86f363d8c7 100644 (file)
@@ -374,25 +374,34 @@ class Pool(object):
         thread = threading.current_thread()
 
         for taskseq, set_length in iter(taskqueue.get, None):
+            task = None
             i = -1
-            for i, task in enumerate(taskseq):
-                if thread._state:
-                    util.debug('task handler found thread._state != RUN')
-                    break
-                try:
-                    put(task)
-                except Exception as e:
-                    job, ind = task[:2]
+            try:
+                for i, task in enumerate(taskseq):
+                    if thread._state:
+                        util.debug('task handler found thread._state != RUN')
+                        break
                     try:
-                        cache[job]._set(ind, (False, e))
-                    except KeyError:
-                        pass
-            else:
+                        put(task)
+                    except Exception as e:
+                        job, ind = task[:2]
+                        try:
+                            cache[job]._set(ind, (False, e))
+                        except KeyError:
+                            pass
+                else:
+                    if set_length:
+                        util.debug('doing set_length()')
+                        set_length(i+1)
+                    continue
+                break
+            except Exception as ex:
+                job, ind = task[:2] if task else (0, 0)
+                if job in cache:
+                    cache[job]._set(ind + 1, (False, ex))
                 if set_length:
                     util.debug('doing set_length()')
                     set_length(i+1)
-                continue
-            break
         else:
             util.debug('task handler got sentinel')
 
index 342688c6349565491f7fc121f7db8844a48bde7b..ff31e3669c17b4e054f7aaa27f421533a363b246 100644 (file)
@@ -1660,6 +1660,14 @@ def sqr(x, wait=0.0):
 def mul(x, y):
     return x*y
 
+class SayWhenError(ValueError): pass
+
+def exception_throwing_generator(total, when):
+    for i in range(total):
+        if i == when:
+            raise SayWhenError("Somebody said when")
+        yield i
+
 class _TestPool(BaseTestCase):
 
     @classmethod
@@ -1758,6 +1766,25 @@ class _TestPool(BaseTestCase):
             self.assertEqual(next(it), i*i)
         self.assertRaises(StopIteration, it.__next__)
 
+    def test_imap_handle_iterable_exception(self):
+        if self.TYPE == 'manager':
+            self.skipTest('test not appropriate for {}'.format(self.TYPE))
+
+        it = self.pool.imap(sqr, exception_throwing_generator(10, 3), 1)
+        for i in range(3):
+            self.assertEqual(next(it), i*i)
+        self.assertRaises(SayWhenError, it.__next__)
+
+        # SayWhenError seen at start of problematic chunk's results
+        it = self.pool.imap(sqr, exception_throwing_generator(20, 7), 2)
+        for i in range(6):
+            self.assertEqual(next(it), i*i)
+        self.assertRaises(SayWhenError, it.__next__)
+        it = self.pool.imap(sqr, exception_throwing_generator(20, 7), 4)
+        for i in range(4):
+            self.assertEqual(next(it), i*i)
+        self.assertRaises(SayWhenError, it.__next__)
+
     def test_imap_unordered(self):
         it = self.pool.imap_unordered(sqr, list(range(1000)))
         self.assertEqual(sorted(it), list(map(sqr, list(range(1000)))))
@@ -1765,6 +1792,25 @@ class _TestPool(BaseTestCase):
         it = self.pool.imap_unordered(sqr, list(range(1000)), chunksize=53)
         self.assertEqual(sorted(it), list(map(sqr, list(range(1000)))))
 
+    def test_imap_unordered_handle_iterable_exception(self):
+        if self.TYPE == 'manager':
+            self.skipTest('test not appropriate for {}'.format(self.TYPE))
+
+        it = self.pool.imap_unordered(sqr,
+                                      exception_throwing_generator(10, 3),
+                                      1)
+        with self.assertRaises(SayWhenError):
+            # imap_unordered makes it difficult to anticipate the SayWhenError
+            for i in range(10):
+                self.assertEqual(next(it), i*i)
+
+        it = self.pool.imap_unordered(sqr,
+                                      exception_throwing_generator(20, 7),
+                                      2)
+        with self.assertRaises(SayWhenError):
+            for i in range(20):
+                self.assertEqual(next(it), i*i)
+
     def test_make_pool(self):
         self.assertRaises(ValueError, multiprocessing.Pool, -1)
         self.assertRaises(ValueError, multiprocessing.Pool, 0)
index cf25d6f8015be275fbca1bfae22a610e3033cc12..a23b470d2aad9e1ec4cfbebb6d8391e38c576a54 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -332,6 +332,7 @@ Raghuram Devarakonda
 Caleb Deveraux
 Catherine Devlin
 Scott Dial
+Alon Diamant
 Toby Dickenson
 Mark Dickinson
 Jack Diederich
@@ -1081,6 +1082,7 @@ Martin Pool
 Iustin Pop
 Claudiu Popa
 John Popplewell
+Davin Potts
 Guillaume Pratte
 Amrit Prem
 Paul Prescod
index 282492c111577501e6a7a3cdd52c2cc30bdc24f2..793cc107417de54a851792dc6ccd421f6775f592 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -18,6 +18,10 @@ Core and Builtins
 Library
 -------
 
+- Issue #23051: multiprocessing.Pool methods imap() and imap_unordered() now
+  handle exceptions raised by an iterator.  Patch by Alon Diamant and Davin
+  Potts.
+
 - Issue #22928: Disabled HTTP header injections in http.client.
   Original patch by Demian Brecht.