Skip to content

Commit 78150c6

Browse files
[3.11] gh-111284: Make multiprocessing tests with threads faster and more reliable (GH-111285) (GH-111511)
(cherry picked from commit 624ace5) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent 9a12c23 commit 78150c6

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

Lib/test/_test_multiprocessing.py

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2437,8 +2437,11 @@ def test_namespace(self):
24372437
#
24382438
#
24392439

2440-
def sqr(x, wait=0.0):
2441-
time.sleep(wait)
2440+
def sqr(x, wait=0.0, event=None):
2441+
if event is None:
2442+
time.sleep(wait)
2443+
else:
2444+
event.wait(wait)
24422445
return x*x
24432446

24442447
def mul(x, y):
@@ -2577,10 +2580,18 @@ def test_async(self):
25772580
self.assertTimingAlmostEqual(get.elapsed, TIMEOUT1)
25782581

25792582
def test_async_timeout(self):
2580-
res = self.pool.apply_async(sqr, (6, TIMEOUT2 + support.SHORT_TIMEOUT))
2581-
get = TimingWrapper(res.get)
2582-
self.assertRaises(multiprocessing.TimeoutError, get, timeout=TIMEOUT2)
2583-
self.assertTimingAlmostEqual(get.elapsed, TIMEOUT2)
2583+
p = self.Pool(3)
2584+
try:
2585+
event = threading.Event() if self.TYPE == 'threads' else None
2586+
res = p.apply_async(sqr, (6, TIMEOUT2 + support.SHORT_TIMEOUT, event))
2587+
get = TimingWrapper(res.get)
2588+
self.assertRaises(multiprocessing.TimeoutError, get, timeout=TIMEOUT2)
2589+
self.assertTimingAlmostEqual(get.elapsed, TIMEOUT2)
2590+
finally:
2591+
if event is not None:
2592+
event.set()
2593+
p.terminate()
2594+
p.join()
25842595

25852596
def test_imap(self):
25862597
it = self.pool.imap(sqr, list(range(10)))
@@ -2682,10 +2693,11 @@ def test_make_pool(self):
26822693

26832694
def test_terminate(self):
26842695
# Simulate slow tasks which take "forever" to complete
2696+
p = self.Pool(3)
26852697
args = [support.LONG_TIMEOUT for i in range(10_000)]
2686-
result = self.pool.map_async(time.sleep, args, chunksize=1)
2687-
self.pool.terminate()
2688-
self.pool.join()
2698+
result = p.map_async(time.sleep, args, chunksize=1)
2699+
p.terminate()
2700+
p.join()
26892701

26902702
def test_empty_iterable(self):
26912703
# See Issue 12157

0 commit comments

Comments
 (0)