Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e41682b

Browse files
committed
Issue #12157: pool.map() does not handle empty iterable correctly
Initial patch by mouad
1 parent a3a164a commit e41682b

3 files changed

Lines changed: 19 additions & 3 deletions

File tree

Lib/multiprocessing/pool.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,7 @@ def __init__(self, cache, chunksize, length, callback, error_callback):
584584
if chunksize <= 0:
585585
self._number_left = 0
586586
self._ready = True
587+
del cache[self._job]
587588
else:
588589
self._number_left = length//chunksize + bool(length % chunksize)
589590

Lib/test/test_multiprocessing.py

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1178,6 +1178,18 @@ def test_terminate(self):
11781178
join()
11791179
self.assertLess(join.elapsed, 0.5)
11801180

1181+
def test_empty_iterable(self):
1182+
# See Issue 12157
1183+
p = self.Pool(1)
1184+
1185+
self.assertEqual(p.map(sqr, []), [])
1186+
self.assertEqual(list(p.imap(sqr, [])), [])
1187+
self.assertEqual(list(p.imap_unordered(sqr, [])), [])
1188+
self.assertEqual(p.map_async(sqr, []).get(), [])
1189+
1190+
p.close()
1191+
p.join()
1192+
11811193
def raising():
11821194
raise KeyError("key")
11831195

@@ -2176,7 +2188,7 @@ class ProcessesMixin(object):
21762188
'Queue', 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore',
21772189
'Condition', 'Event', 'Value', 'Array', 'RawValue',
21782190
'RawArray', 'current_process', 'active_children', 'Pipe',
2179-
'connection', 'JoinableQueue'
2191+
'connection', 'JoinableQueue', 'Pool'
21802192
)))
21812193

21822194
testcases_processes = create_test_cases(ProcessesMixin, type='processes')
@@ -2190,7 +2202,7 @@ class ManagerMixin(object):
21902202
locals().update(get_attributes(manager, (
21912203
'Queue', 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore',
21922204
'Condition', 'Event', 'Value', 'Array', 'list', 'dict',
2193-
'Namespace', 'JoinableQueue'
2205+
'Namespace', 'JoinableQueue', 'Pool'
21942206
)))
21952207

21962208
testcases_manager = create_test_cases(ManagerMixin, type='manager')
@@ -2204,7 +2216,7 @@ class ThreadsMixin(object):
22042216
'Queue', 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore',
22052217
'Condition', 'Event', 'Value', 'Array', 'current_process',
22062218
'active_children', 'Pipe', 'connection', 'dict', 'list',
2207-
'Namespace', 'JoinableQueue'
2219+
'Namespace', 'JoinableQueue', 'Pool'
22082220
)))
22092221

22102222
testcases_threads = create_test_cases(ThreadsMixin, type='threads')

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,9 @@ Core and Builtins
7070
Library
7171
-------
7272

73+
- Issue #12157: Make pool.map() empty iterables correctly. Initial
74+
patch by mouad.
75+
7376
- Issue #14992: os.makedirs(path, exist_ok=True) would raise an OSError
7477
when the path existed and had the S_ISGID mode bit set when it was
7578
not explicitly asked for. This is no longer an exception as mkdir

0 commit comments

Comments
 (0)