@@ -1660,6 +1660,14 @@ def sqr(x, wait=0.0):
16601660def mul (x , y ):
16611661 return x * y
16621662
1663+ class SayWhenError (ValueError ): pass
1664+
1665+ def exception_throwing_generator (total , when ):
1666+ for i in range (total ):
1667+ if i == when :
1668+ raise SayWhenError ("Somebody said when" )
1669+ yield i
1670+
16631671class _TestPool (BaseTestCase ):
16641672
16651673 @classmethod
@@ -1758,13 +1766,51 @@ def test_imap(self):
17581766 self .assertEqual (next (it ), i * i )
17591767 self .assertRaises (StopIteration , it .__next__ )
17601768
1769+ def test_imap_handle_iterable_exception (self ):
1770+ if self .TYPE == 'manager' :
1771+ self .skipTest ('test not appropriate for {}' .format (self .TYPE ))
1772+
1773+ it = self .pool .imap (sqr , exception_throwing_generator (10 , 3 ), 1 )
1774+ for i in range (3 ):
1775+ self .assertEqual (next (it ), i * i )
1776+ self .assertRaises (SayWhenError , it .__next__ )
1777+
1778+ # SayWhenError seen at start of problematic chunk's results
1779+ it = self .pool .imap (sqr , exception_throwing_generator (20 , 7 ), 2 )
1780+ for i in range (6 ):
1781+ self .assertEqual (next (it ), i * i )
1782+ self .assertRaises (SayWhenError , it .__next__ )
1783+ it = self .pool .imap (sqr , exception_throwing_generator (20 , 7 ), 4 )
1784+ for i in range (4 ):
1785+ self .assertEqual (next (it ), i * i )
1786+ self .assertRaises (SayWhenError , it .__next__ )
1787+
17611788 def test_imap_unordered (self ):
17621789 it = self .pool .imap_unordered (sqr , list (range (1000 )))
17631790 self .assertEqual (sorted (it ), list (map (sqr , list (range (1000 )))))
17641791
17651792 it = self .pool .imap_unordered (sqr , list (range (1000 )), chunksize = 53 )
17661793 self .assertEqual (sorted (it ), list (map (sqr , list (range (1000 )))))
17671794
1795+ def test_imap_unordered_handle_iterable_exception (self ):
1796+ if self .TYPE == 'manager' :
1797+ self .skipTest ('test not appropriate for {}' .format (self .TYPE ))
1798+
1799+ it = self .pool .imap_unordered (sqr ,
1800+ exception_throwing_generator (10 , 3 ),
1801+ 1 )
1802+ with self .assertRaises (SayWhenError ):
1803+ # imap_unordered makes it difficult to anticipate the SayWhenError
1804+ for i in range (10 ):
1805+ self .assertEqual (next (it ), i * i )
1806+
1807+ it = self .pool .imap_unordered (sqr ,
1808+ exception_throwing_generator (20 , 7 ),
1809+ 2 )
1810+ with self .assertRaises (SayWhenError ):
1811+ for i in range (20 ):
1812+ self .assertEqual (next (it ), i * i )
1813+
17681814 def test_make_pool (self ):
17691815 self .assertRaises (ValueError , multiprocessing .Pool , - 1 )
17701816 self .assertRaises (ValueError , multiprocessing .Pool , 0 )
0 commit comments