]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
Issue #23051: multiprocessing.Pool methods imap() and imap_unordered() now
authorSerhiy Storchaka <storchaka@gmail.com>
Fri, 13 Mar 2015 06:31:34 +0000 (08:31 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Fri, 13 Mar 2015 06:31:34 +0000 (08:31 +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 04531b91bd23c314d18ddffd26d202f533719ae2..991f87f2f1b572f0a020c8205b021af5dd697c7f 100644 (file)
@@ -334,25 +334,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:
-                    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:
+                        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:
+                        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:
                     debug('doing set_length()')
                     set_length(i+1)
-                continue
-            break
         else:
             debug('task handler got sentinel')
 
index b1e75b566325b07cac458ccd2983a45d5f890c43..681529ff7d42641c2dd28f53edc795e4d7bacdcf 100644 (file)
@@ -1122,6 +1122,15 @@ class _TestContainers(BaseTestCase):
 def sqr(x, wait=0.0):
     time.sleep(wait)
     return x*x
+
+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):
 
     def test_apply(self):
@@ -1177,6 +1186,25 @@ class _TestPool(BaseTestCase):
             self.assertEqual(it.next(), 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, range(1000))
         self.assertEqual(sorted(it), map(sqr, range(1000)))
@@ -1184,6 +1212,25 @@ class _TestPool(BaseTestCase):
         it = self.pool.imap_unordered(sqr, range(1000), chunksize=53)
         self.assertEqual(sorted(it), map(sqr, 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 e91e2a4f558ca304fa2fb53c7aa6bb2e82f471f1..752b83a897217601ed451cfa7963751175f31ba4 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -325,6 +325,7 @@ Raghuram Devarakonda
 Caleb Deveraux
 Catherine Devlin
 Scott Dial
+Alon Diamant
 Toby Dickenson
 Mark Dickinson
 Jack Diederich
index 0be5fdbef1755eeb7f4ea5e633523189f8d4117c..a364f5786e91eef560b67e88671489ece8958670 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -21,6 +21,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 httplib.
   Original patch by Demian Brecht.