mirror of
https://github.com/python/cpython.git
synced 2025-09-26 18:29:57 +00:00
[3.12] gh-111284: Make multiprocessing tests with threads faster and more reliable (GH-111285) (GH-111510)
(cherry picked from commit 624ace5a2f
)
Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
This commit is contained in:
parent
748bc48648
commit
7ac2c53333
1 changed files with 21 additions and 9 deletions
|
@ -2438,8 +2438,11 @@ class _TestContainers(BaseTestCase):
|
||||||
#
|
#
|
||||||
#
|
#
|
||||||
|
|
||||||
def sqr(x, wait=0.0):
|
def sqr(x, wait=0.0, event=None):
|
||||||
time.sleep(wait)
|
if event is None:
|
||||||
|
time.sleep(wait)
|
||||||
|
else:
|
||||||
|
event.wait(wait)
|
||||||
return x*x
|
return x*x
|
||||||
|
|
||||||
def mul(x, y):
|
def mul(x, y):
|
||||||
|
@ -2578,10 +2581,18 @@ class _TestPool(BaseTestCase):
|
||||||
self.assertTimingAlmostEqual(get.elapsed, TIMEOUT1)
|
self.assertTimingAlmostEqual(get.elapsed, TIMEOUT1)
|
||||||
|
|
||||||
def test_async_timeout(self):
|
def test_async_timeout(self):
|
||||||
res = self.pool.apply_async(sqr, (6, TIMEOUT2 + support.SHORT_TIMEOUT))
|
p = self.Pool(3)
|
||||||
get = TimingWrapper(res.get)
|
try:
|
||||||
self.assertRaises(multiprocessing.TimeoutError, get, timeout=TIMEOUT2)
|
event = threading.Event() if self.TYPE == 'threads' else None
|
||||||
self.assertTimingAlmostEqual(get.elapsed, TIMEOUT2)
|
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):
|
def test_imap(self):
|
||||||
it = self.pool.imap(sqr, list(range(10)))
|
it = self.pool.imap(sqr, list(range(10)))
|
||||||
|
@ -2683,10 +2694,11 @@ class _TestPool(BaseTestCase):
|
||||||
|
|
||||||
def test_terminate(self):
|
def test_terminate(self):
|
||||||
# Simulate slow tasks which take "forever" to complete
|
# Simulate slow tasks which take "forever" to complete
|
||||||
|
p = self.Pool(3)
|
||||||
args = [support.LONG_TIMEOUT for i in range(10_000)]
|
args = [support.LONG_TIMEOUT for i in range(10_000)]
|
||||||
result = self.pool.map_async(time.sleep, args, chunksize=1)
|
result = p.map_async(time.sleep, args, chunksize=1)
|
||||||
self.pool.terminate()
|
p.terminate()
|
||||||
self.pool.join()
|
p.join()
|
||||||
|
|
||||||
def test_empty_iterable(self):
|
def test_empty_iterable(self):
|
||||||
# See Issue 12157
|
# See Issue 12157
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue