Skip to content

Commit

Permalink
pythongh-111284: Make multiprocessing tests with threads faster and m…
Browse files Browse the repository at this point in the history
…ore reliable (pythonGH-111285)
  • Loading branch information
serhiy-storchaka authored and FullteaR committed Nov 3, 2023
1 parent 2efd0c7 commit 9ff5c68
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions Lib/test/_test_multiprocessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2438,8 +2438,11 @@ def test_namespace(self):
#
#

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):
Expand Down Expand Up @@ -2578,10 +2581,18 @@ def test_async(self):
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)))
Expand Down Expand Up @@ -2683,10 +2694,11 @@ def test_make_pool(self):

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
Expand Down

0 comments on commit 9ff5c68

Please sign in to comment.