diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2025-07-15 05:33:33 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2025-07-15 05:33:33 (GMT) |
commit | 55eaaab8a4064ef76013a82c16c9991eba30dfeb (patch) | |
tree | 8d434abcb1dbad9a6b7deb089fdedee45b604578 /Lib/test/support/threading_helper.py | |
parent | bbbbb2e2d15794bfcaacfdf2bce43ca7032fe50f (diff) | |
download | cpython-55eaaab8a4064ef76013a82c16c9991eba30dfeb.zip cpython-55eaaab8a4064ef76013a82c16c9991eba30dfeb.tar.gz cpython-55eaaab8a4064ef76013a82c16c9991eba30dfeb.tar.bz2 |
[3.14] gh-116738: Make grp module thread-safe (GH-135434) (#136658)
gh-116738: Make grp module thread-safe (GH-135434)
Make grp module methods getgrgid() and getgrnam() thread-safe when the GIL is disabled and getgrgid_r()/getgrnam_r() C APIs are not available.
---------
(cherry picked from commit 9363703bd3bf86e363c14a02e3d729caf1e29f44)
Co-authored-by: Alper <alperyoney@fb.com>
Co-authored-by: Kumar Aditya <kumaraditya@python.org>
Diffstat (limited to 'Lib/test/support/threading_helper.py')
-rw-r--r-- | Lib/test/support/threading_helper.py | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/Lib/test/support/threading_helper.py b/Lib/test/support/threading_helper.py index afa25a7..3e04c34 100644 --- a/Lib/test/support/threading_helper.py +++ b/Lib/test/support/threading_helper.py @@ -248,3 +248,27 @@ def requires_working_threading(*, module=False): raise unittest.SkipTest(msg) else: return unittest.skipUnless(can_start_thread, msg) + + +def run_concurrently(worker_func, nthreads, args=(), kwargs={}): + """ + Run the worker function concurrently in multiple threads. + """ + barrier = threading.Barrier(nthreads) + + def wrapper_func(*args, **kwargs): + # Wait for all threads to reach this point before proceeding. + barrier.wait() + worker_func(*args, **kwargs) + + with catch_threading_exception() as cm: + workers = [ + threading.Thread(target=wrapper_func, args=args, kwargs=kwargs) + for _ in range(nthreads) + ] + with start_threads(workers): + pass + + # If a worker thread raises an exception, re-raise it. + if cm.exc_value is not None: + raise cm.exc_value |