Skip to content

Commit 2b7fc1b

Browse files
GH-83658: make multiprocessing.Pool raise an exception if maxtasksperchild is not None or a positive int (GH-93364) (GH-93923)
1 parent 05d83a7 commit 2b7fc1b

File tree

3 files changed

+9
-0
lines changed

3 files changed

+9
-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
@@ -2861,6 +2861,11 @@ def test_pool_worker_lifetime_early_close(self):
28612861
for (j, res) in enumerate(results):
28622862
self.assertEqual(res.get(), sqr(j))
28632863

2864+
def test_pool_maxtasksperchild_invalid(self):
2865+
for value in [0, -1, 0.5, "12"]:
2866+
with self.assertRaises(ValueError):
2867+
multiprocessing.Pool(3, maxtasksperchild=value)
2868+
28642869
def test_worker_finalization_via_atexit_handler_of_multiprocessing(self):
28652870
# tests cases against bpo-38744 and bpo-39360
28662871
cmd = '''if 1:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Make :class:`multiprocessing.Pool` raise an exception if ``maxtasksperchild`` is not ``None`` or a positive int.

0 commit comments

Comments
 (0)