[Python-checkins] GH-83658: make multiprocessing.Pool raise an exception if maxtasksperchild is not None or a positive int (GH-93364) (GH-93924)
iritkatriel
webhook-mailer at python.org
Fri Jun 17 18:32:20 EDT 2022
https://github.com/python/cpython/commit/2d33d217aaf68fd8dc5d3eebc048baf82924c18a
commit: 2d33d217aaf68fd8dc5d3eebc048baf82924c18a
branch: 3.10
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: iritkatriel <1055913+iritkatriel at users.noreply.github.com>
date: 2022-06-17T23:32:15+01:00
summary:
GH-83658: make multiprocessing.Pool raise an exception if maxtasksperchild is not None or a positive int (GH-93364) (GH-93924)
Closes GH-83658.
(cherry picked from commit e37a158725dec561f234b81864363d55f05c7b4e)
Co-authored-by: Irit Katriel <1055913+iritkatriel at users.noreply.github.com>
Co-authored-by: Irit Katriel <1055913+iritkatriel at users.noreply.github.com>
files:
A Misc/NEWS.d/next/Library/2022-05-30-21-42-50.gh-issue-83658.01Ntx0.rst
M Lib/multiprocessing/pool.py
M Lib/test/_test_multiprocessing.py
diff --git a/Lib/multiprocessing/pool.py b/Lib/multiprocessing/pool.py
index bbe05a550c349..961d7e5991847 100644
--- a/Lib/multiprocessing/pool.py
+++ b/Lib/multiprocessing/pool.py
@@ -203,6 +203,9 @@ def __init__(self, processes=None, initializer=None, initargs=(),
processes = os.cpu_count() or 1
if processes < 1:
raise ValueError("Number of processes must be at least 1")
+ if maxtasksperchild is not None:
+ if not isinstance(maxtasksperchild, int) or maxtasksperchild <= 0:
+ raise ValueError("maxtasksperchild must be a positive int or None")
if initializer is not None and not callable(initializer):
raise TypeError('initializer must be a callable')
diff --git a/Lib/test/_test_multiprocessing.py b/Lib/test/_test_multiprocessing.py
index 1b55cb31c3030..ba488b17920ef 100644
--- a/Lib/test/_test_multiprocessing.py
+++ b/Lib/test/_test_multiprocessing.py
@@ -2805,6 +2805,11 @@ def test_pool_worker_lifetime_early_close(self):
for (j, res) in enumerate(results):
self.assertEqual(res.get(), sqr(j))
+ def test_pool_maxtasksperchild_invalid(self):
+ for value in [0, -1, 0.5, "12"]:
+ with self.assertRaises(ValueError):
+ multiprocessing.Pool(3, maxtasksperchild=value)
+
def test_worker_finalization_via_atexit_handler_of_multiprocessing(self):
# tests cases against bpo-38744 and bpo-39360
cmd = '''if 1:
diff --git a/Misc/NEWS.d/next/Library/2022-05-30-21-42-50.gh-issue-83658.01Ntx0.rst b/Misc/NEWS.d/next/Library/2022-05-30-21-42-50.gh-issue-83658.01Ntx0.rst
new file mode 100644
index 0000000000000..a187309540980
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2022-05-30-21-42-50.gh-issue-83658.01Ntx0.rst
@@ -0,0 +1 @@
+Make :class:`multiprocessing.Pool` raise an exception if ``maxtasksperchild`` is not ``None`` or a positive int.
More information about the Python-checkins
mailing list