diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2023-10-30 17:44:27 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-30 17:44:27 (GMT) |
commit | 78150c6f7bf2b21f0bddaa87e6ab7b2fbaa4a6db (patch) | |
tree | 2111013d0a39dbb67d2bbcba936ba9678e656f9a | |
parent | 9a12c23fe417efc21b0c5412355bddbf0c3c6324 (diff) | |
download | cpython-78150c6f7bf2b21f0bddaa87e6ab7b2fbaa4a6db.zip cpython-78150c6f7bf2b21f0bddaa87e6ab7b2fbaa4a6db.tar.gz cpython-78150c6f7bf2b21f0bddaa87e6ab7b2fbaa4a6db.tar.bz2 |
[3.11] gh-111284: Make multiprocessing tests with threads faster and more reliable (GH-111285) (GH-111511)
(cherry picked from commit 624ace5a2f02715d084c29eaf2211cd0dd550690)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
-rw-r--r-- | Lib/test/_test_multiprocessing.py | 30 |
1 files changed, 21 insertions, 9 deletions
diff --git a/Lib/test/_test_multiprocessing.py b/Lib/test/_test_multiprocessing.py index 3bcb1d1..3d581d6 100644 --- a/Lib/test/_test_multiprocessing.py +++ b/Lib/test/_test_multiprocessing.py @@ -2437,8 +2437,11 @@ class _TestContainers(BaseTestCase): # # -def sqr(x, wait=0.0): - time.sleep(wait) +def sqr(x, wait=0.0, event=None): + if event is None: + time.sleep(wait) + else: + event.wait(wait) return x*x def mul(x, y): @@ -2577,10 +2580,18 @@ class _TestPool(BaseTestCase): self.assertTimingAlmostEqual(get.elapsed, TIMEOUT1) def test_async_timeout(self): - res = self.pool.apply_async(sqr, (6, TIMEOUT2 + support.SHORT_TIMEOUT)) - get = TimingWrapper(res.get) - self.assertRaises(multiprocessing.TimeoutError, get, timeout=TIMEOUT2) - self.assertTimingAlmostEqual(get.elapsed, TIMEOUT2) + p = self.Pool(3) + try: + event = threading.Event() if self.TYPE == 'threads' else None + res = p.apply_async(sqr, (6, TIMEOUT2 + support.SHORT_TIMEOUT, event)) + get = TimingWrapper(res.get) + self.assertRaises(multiprocessing.TimeoutError, get, timeout=TIMEOUT2) + self.assertTimingAlmostEqual(get.elapsed, TIMEOUT2) + finally: + if event is not None: + event.set() + p.terminate() + p.join() def test_imap(self): it = self.pool.imap(sqr, list(range(10))) @@ -2682,10 +2693,11 @@ class _TestPool(BaseTestCase): def test_terminate(self): # Simulate slow tasks which take "forever" to complete + p = self.Pool(3) args = [support.LONG_TIMEOUT for i in range(10_000)] - result = self.pool.map_async(time.sleep, args, chunksize=1) - self.pool.terminate() - self.pool.join() + result = p.map_async(time.sleep, args, chunksize=1) + p.terminate() + p.join() def test_empty_iterable(self): # See Issue 12157 |