Skip to content

Commit ad25815

Browse files
committed
pythongh-83658: make multiprocessing.Pool raise an exception if maxtasksperchild is not None or a positive int
1 parent d853758 commit ad25815

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

Lib/multiprocessing/pool.py

+3
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,9 @@ def __init__(self, processes=None, initializer=None, initargs=(),
203203
processes = os.cpu_count() or 1
204204
if processes < 1:
205205
raise ValueError("Number of processes must be at least 1")
206+
if maxtasksperchild is not None:
207+
if not isinstance(maxtasksperchild, int) or maxtasksperchild <= 0:
208+
raise ValueError("maxtasksperchild must be a positive int or None")
206209

207210
if initializer is not None and not callable(initializer):
208211
raise TypeError('initializer must be a callable')

Lib/test/_test_multiprocessing.py

+5
Original file line numberDiff line numberDiff line change
@@ -2860,6 +2860,11 @@ def test_pool_worker_lifetime_early_close(self):
28602860
for (j, res) in enumerate(results):
28612861
self.assertEqual(res.get(), sqr(j))
28622862

2863+
def test_pool_maxtasksperchild_invalid(self):
2864+
for value in [0, -1, 0.5, "12"]:
2865+
with self.assertRaises(ValueError):
2866+
multiprocessing.Pool(3, maxtasksperchild=value)
2867+
28632868
def test_worker_finalization_via_atexit_handler_of_multiprocessing(self):
28642869
# tests cases against bpo-38744 and bpo-39360
28652870
cmd = '''if 1:

0 commit comments

Comments
 (0)