Skip to content

Commit e37a158

Browse files
authored
GH-83658: make multiprocessing.Pool raise an exception if maxtasksperchild is not None or a positive int (GH-93364)
Closes #83658.
1 parent 38af903 commit e37a158

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

2866+
def test_pool_maxtasksperchild_invalid(self):
2867+
for value in [0, -1, 0.5, "12"]:
2868+
with self.assertRaises(ValueError):
2869+
multiprocessing.Pool(3, maxtasksperchild=value)
2870+
28662871
def test_worker_finalization_via_atexit_handler_of_multiprocessing(self):
28672872
# tests cases against bpo-38744 and bpo-39360
28682873
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)