diff options
-rw-r--r-- | Lib/multiprocessing/pool.py | 3 | ||||
-rw-r--r-- | Lib/test/_test_multiprocessing.py | 5 | ||||
-rw-r--r-- | Misc/NEWS.d/next/Library/2022-05-30-21-42-50.gh-issue-83658.01Ntx0.rst | 1 |
3 files changed, 9 insertions, 0 deletions
diff --git a/Lib/multiprocessing/pool.py b/Lib/multiprocessing/pool.py index bbe05a5..961d7e5 100644 --- a/Lib/multiprocessing/pool.py +++ b/Lib/multiprocessing/pool.py @@ -203,6 +203,9 @@ class Pool(object): 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 b20bc0b..7596997 100644 --- a/Lib/test/_test_multiprocessing.py +++ b/Lib/test/_test_multiprocessing.py @@ -2863,6 +2863,11 @@ class _TestPoolWorkerLifetime(BaseTestCase): 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 0000000..a187309 --- /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. |