URL: http://github.com/python/cpython/pull/125663.diff
ListEqual(list(res), ["0", "1", "2", "3"]) + + def test_map_buffersize_on_multiple_iterables(self): + ints = range(4) + for buffersize in (1, 2, len(ints), len(ints) * 2): + with self.subTest(buffersize=buffersize): + res = self.executor.map(add, ints, ints, buffersize=buffersize) + self.assertListEqual(list(res), [0, 2, 4, 6]) + + def test_map_buffersize_on_infinite_iterable(self): + res = self.executor.map(str, itertools.count(), buffersize=2) + self.assertEqual(next(res, None), "0") + self.assertEqual(next(res, None), "1") + self.assertEqual(next(res, None), "2") + + def test_map_buffersize_on_multiple_infinite_iterables(self): + res = self.executor.map( + add, + itertools.count(), + itertools.count(), + buffersize=2 + ) + self.assertEqual(next(res, None), 0) + self.assertEqual(next(res, None), 2) + self.assertEqual(next(res, None), 4) + + def test_map_buffersize_on_empty_iterable(self): + res = self.executor.map(str, [], buffersize=2) + self.assertIsNone(next(res, None)) + + def test_map_buffersize_without_iterable(self): + res = self.executor.map(str, buffersize=2) + self.assertIsNone(next(res, None)) + + def test_map_buffersize_when_buffer_is_full(self): + ints = iter(range(4)) + buffersize = 2 + self.executor.map(str, ints, buffersize=buffersize) + self.executor.shutdown(wait=True) # wait for tasks to complete + self.assertEqual( + next(ints), + buffersize, + msg="should have fetched only `buffersize` elements from `ints`.", + ) + def test_shutdown_race_issue12456(self): # Issue #12456: race condition at shutdown where trying to post a # sentinel in the call queue blocks (the queue is full while processes diff --git a/Misc/NEWS.d/next/Library/2024-10-18-10-27-54.gh-issue-74028.4d4vVD.rst b/Misc/NEWS.d/next/Library/2024-10-18-10-27-54.gh-issue-74028.4d4vVD.rst new file mode 100644 index 00000000000000..6760e2b935430c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2024-10-18-10-27-54.gh-issue-74028.4d4vVD.rst @@ -0,0 +1,4 @@ +Add the optional ``buffersize`` parameter to +:meth:`concurrent.futures.Executor.map` to limit the number of submitted tasks +whose results have not yet been yielded. If the buffer is full, iteration over +the *iterables* pauses until a result is yielded from the buffer.Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: