mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
Issue #23799: Added test.support.start_threads() for running and cleaning up
multiple threads.
This commit is contained in:
parent
8218bd4caf
commit
263dcd20a3
9 changed files with 77 additions and 80 deletions
|
@ -6,6 +6,7 @@ if __name__ != 'test.support':
|
|||
import collections.abc
|
||||
import contextlib
|
||||
import errno
|
||||
import faulthandler
|
||||
import fnmatch
|
||||
import functools
|
||||
import gc
|
||||
|
@ -96,7 +97,7 @@ __all__ = [
|
|||
# logging
|
||||
"TestHandler",
|
||||
# threads
|
||||
"threading_setup", "threading_cleanup",
|
||||
"threading_setup", "threading_cleanup", "reap_threads", "start_threads",
|
||||
# miscellaneous
|
||||
"check_warnings", "EnvironmentVarGuard", "run_with_locale", "swap_item",
|
||||
"swap_attr", "Matcher", "set_memlimit", "SuppressCrashReport", "sortdict",
|
||||
|
@ -1940,6 +1941,42 @@ def reap_children():
|
|||
except:
|
||||
break
|
||||
|
||||
@contextlib.contextmanager
|
||||
def start_threads(threads, unlock=None):
|
||||
threads = list(threads)
|
||||
started = []
|
||||
try:
|
||||
try:
|
||||
for t in threads:
|
||||
t.start()
|
||||
started.append(t)
|
||||
except:
|
||||
if verbose:
|
||||
print("Can't start %d threads, only %d threads started" %
|
||||
(len(threads), len(started)))
|
||||
raise
|
||||
yield
|
||||
finally:
|
||||
try:
|
||||
if unlock:
|
||||
unlock()
|
||||
endtime = starttime = time.time()
|
||||
for timeout in range(1, 16):
|
||||
endtime += 60
|
||||
for t in started:
|
||||
t.join(max(endtime - time.time(), 0.01))
|
||||
started = [t for t in started if t.isAlive()]
|
||||
if not started:
|
||||
break
|
||||
if verbose:
|
||||
print('Unable to join %d threads during a period of '
|
||||
'%d minutes' % (len(started), timeout))
|
||||
finally:
|
||||
started = [t for t in started if t.isAlive()]
|
||||
if started:
|
||||
faulthandler.dump_traceback(sys.stdout)
|
||||
raise AssertionError('Unable to join %d threads' % len(started))
|
||||
|
||||
@contextlib.contextmanager
|
||||
def swap_attr(obj, attr, new_val):
|
||||
"""Temporary swap out an attribute with a new object.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue