8000 gh-116738: Move run_concurrently() to threading_helper · python/cpython@dedbae9 · GitHub
[go: up one dir, main page]

Skip to content

Commit dedbae9

Browse files
committed
gh-116738: Move run_concurrently() to threading_helper
1 parent 0e0dda0 commit dedbae9

File tree

4 files changed

+26
-54
lines changed

4 files changed

+26
-54
lines changed

Lib/test/support/threading_helper.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,27 @@ def requires_working_threading(*, module=False):
248248
raise unittest.SkipTest(msg)
249249
else:
250250
return unittest.skipUnless(can_start_thread, msg)
251+
252+
253+
def run_concurrently(worker_func, args, nthreads):
254+
"""
255+
Run the worker function concurrently in multiple threads.
256+
"""
257+
barrier = threading.Barrier(nthreads)
258+
259+
def wrapper_func(*args):
260+
# Wait for all threads to reach this point before proceeding.
261+
barrier.wait()
262+
worker_func(*args)
263+
264+
with catch_threading_exception() as cm:
265+
workers = (
266+
threading.Thread(target=wrapper_func, args=args)
267+
for _ in range(nthreads)
268+
)
269+
with start_threads(workers):
270+
pass
271+
272+
# If a worker thread raises an exception, re-raise it.
273+
if cm.exc_value is not None:
274+
raise cm.exc_value

Lib/test/test_free_threading/test_ft.py

Lines changed: 0 additions & 52 deletions
This file was deleted.

Lib/test/test_free_threading/test_grp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import unittest
22

33
from test.support import import_helper, threading_helper
4-
from test.test_free_threading.test_ft import run_concurrently
4+
from test.support.threading_helper import run_concurrently
55

66
grp = import_helper.import_module("grp")
77

Lib/test/test_free_threading/test_heapq.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from random import shuffle, randint
77

88
from test.support import threading_helper
9-
from test.test_free_threading.test_ft import run_concurrently
9+
from test.support.threading_helper import run_concurrently
1010
from test import test_heapq
1111

1212

0 commit comments

Comments
 (0)
0