Skip to content

Commit a49809d

Browse files
iritkatrielmiss-islington
authored andcommitted
pythonGH-83658: make multiprocessing.Pool raise an exception if maxtasksperchild is not None or a positive int (pythonGH-93364)
Closes pythonGH-83658. (cherry picked from commit e37a158) Co-authored-by: Irit Katriel <[email protected]>
1 parent 5163a25 commit a49809d

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
@@ -2805,6 +2805,11 @@ def test_pool_worker_lifetime_early_close(self):
28052805
for (j, res) in enumerate(results):
28062806
self.assertEqual(res.get(), sqr(j))
28072807

2808+
def test_pool_maxtasksperchild_invalid(self):
2809+
for value in [0, -1, 0.5, "12"]:
2810+
with self.assertRaises(ValueError):
2811+
multiprocessing.Pool(3, maxtasksperchild=value)
2812+
28082813
def test_worker_finalization_via_atexit_handler_of_multiprocessing(self):
28092814
# tests cases against bpo-38744 and bpo-39360
28102815
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)