Skip to content

Commit 851caf4

Browse files
committed
gh-116738: Improve run_concurrently() arguments
1 parent e879eed commit 851caf4

File tree

4 files changed

+23
-17
lines changed

4 files changed

+23
-17
lines changed

Doc/library/test.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1384,6 +1384,13 @@ The :mod:`test.support.threading_helper` module provides support for threading t
13841384
.. versionadded:: 3.8
13851385

13861386

1387+
.. function:: run_concurrently(worker_func, nthreads, args=(), kwargs={})
1388+
1389+
Run the worker function concurrently in multiple threads.
1390+
Re-raises an exception if any thread raises one, after all threads have
1391+
finished.
1392+
1393+
13871394
:mod:`test.support.os_helper` --- Utilities for os tests
13881395
========================================================================
13891396

Lib/test/support/threading_helper.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -250,20 +250,20 @@ def requires_working_threading(*, module=False):
250250
return unittest.skipUnless(can_start_thread, msg)
251251

252252

253-
def run_concurrently(worker_func, args, nthreads):
253+
def run_concurrently(worker_func, nthreads, args=(), kwargs={}):
254254
"""
255255
Run the worker function concurrently in multiple threads.
256256
"""
257257
barrier = threading.Barrier(nthreads)
258258

259-
def wrapper_func(*args):
259+
def wrapper_func(*args, **kwargs):
260260
# Wait for all threads to reach this point before proceeding.
261261
barrier.wait()
262-
worker_func(*args)
262+
worker_func(*args, **kwargs)
263263

264264
with catch_threading_exception() as cm:
265265
workers = (
266-
threading.Thread(target=wrapper_func, args=args)
266+
threading.Thread(target=wrapper_func, args=args, kwargs=kwargs)
267267
for _ in range(nthreads)
268268
)
269269
with start_threads(workers):

Lib/test/test_free_threading/test_grp.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,14 @@ def setUp(self):
1919
def test_racing_test_values(self):
2020
# test_grp.test_values() calls grp.getgrall() and checks the entries
2121
run_concurrently(
22-
worker_func=self.test_grp.test_values, args=(), nthreads=NTHREADS
22+
worker_func=self.test_grp.test_values, nthreads=NTHREADS
2323
)
2424

2525
def test_racing_test_values_extended(self):
2626
# test_grp.test_values_extended() calls grp.getgrall(), grp.getgrgid(),
2727
# grp.getgrnam() and checks the entries
2828
run_concurrently(
2929
worker_func=self.test_grp.test_values_extended,
30-
args=(),
3130
nthreads=NTHREADS,
3231
)
3332

Lib/test/test_free_threading/test_heapq.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def test_racing_heapify(self):
3030
shuffle(heap)
3131

3232
run_concurrently(
33-
worker_func=heapq.heapify, args=(heap,), nthreads=NTHREADS
33+
worker_func=heapq.heapify, nthreads=NTHREADS, args=(heap,)
3434
)
3535
self.test_heapq.check_invariant(heap)
3636

@@ -42,7 +42,7 @@ def heappush_func(heap):
4242
heapq.heappush(heap, item)
4343

4444
run_concurrently(
45-
worker_func=heappush_func, args=(heap,), nthreads=NTHREADS
45+
worker_func=heappush_func, nthreads=NTHREADS, args=(heap,)
4646
)
4747
self.test_heapq.check_invariant(heap)
4848

@@ -64,8 +64,8 @@ def heappop_func(heap, pop_count):
6464

6565
run_concurrently(
6666
worker_func=heappop_func,
67-
args=(heap, per_thread_pop_count),
6867
nthreads=NTHREADS,
68+
args=(heap, per_thread_pop_count),
6969
)
7070
self.assertEqual(len(heap), 0)
7171

@@ -80,8 +80,8 @@ def heappushpop_func(heap, pushpop_items):
8080

8181
run_concurrently(
8282
worker_func=heappushpop_func,
83-
args=(heap, pushpop_items),
8483
nthreads=NTHREADS,
84+
args=(heap, pushpop_items),
8585
)
8686
self.assertEqual(len(heap), OBJECT_COUNT)
8787
self.test_heapq.check_invariant(heap)
@@ -96,8 +96,8 @@ def heapreplace_func(heap, replace_items):
9696

9797
run_concurrently(
9898
worker_func=heapreplace_func,
99-
args=(heap, replace_items),
10099
nthreads=NTHREADS,
100+
args=(heap, replace_items),
101101
)
102102
self.assertEqual(len(heap), OBJECT_COUNT)
103103
self.test_heapq.check_invariant(heap)
@@ -107,7 +107,7 @@ def test_racing_heapify_max(self):
107107
shuffle(max_heap)
108108

109109
run_concurrently(
110-
worker_func=heapq.heapify_max, args=(max_heap,), nthreads=NTHREADS
110+
worker_func=heapq.heapify_max, nthreads=NTHREADS, args=(max_heap,)
111111
)
112112
self.test_heapq.check_max_invariant(max_heap)
113113

@@ -119,7 +119,7 @@ def heappush_max_func(max_heap):
119119
heapq.heappush_max(max_heap, item)
120120

121121
run_concurrently(
122-
worker_func=heappush_max_func, args=(max_heap,), nthreads=NTHREADS
122+
worker_func=heappush_max_func, nthreads=NTHREADS, args=(max_heap,)
123123
)
124124
self.test_heapq.check_max_invariant(max_heap)
125125

@@ -141,8 +141,8 @@ def heappop_max_func(max_heap, pop_count):
141141

142142
run_concurrently(
143143
worker_func=heappop_max_func,
144-
args=(max_heap, per_thread_pop_count),
145144
nthreads=NTHREADS,
145+
args=(max_heap, per_thread_pop_count),
146146
)
147147
self.assertEqual(len(max_heap), 0)
148148

@@ -157,8 +157,8 @@ def heappushpop_max_func(max_heap, pushpop_items):
157157

158158
run_concurrently(
159159
worker_func=heappushpop_max_func,
160-
args=(max_heap, pushpop_items),
161160
nthreads=NTHREADS,
161+
args=(max_heap, pushpop_items),
162162
)
163163
self.assertEqual(len(max_heap), OBJECT_COUNT)
164164
self.test_heapq.check_max_invariant(max_heap)
@@ -173,8 +173,8 @@ def heapreplace_max_func(max_heap, replace_items):
173173

174174
run_concurrently(
175175
worker_func=heapreplace_max_func,
176-
args=(max_heap, replace_items),
177176
nthreads=NTHREADS,
177+
args=(max_heap, replace_items),
178178
)
179179
self.assertEqual(len(max_heap), OBJECT_COUNT)
180180
self.test_heapq.check_max_invariant(max_heap)
@@ -204,7 +204,7 @@ def worker():
204204
except IndexError:
205205
pass
206206

207-
run_concurrently(worker, (), n_threads * 2)
207+
run_concurrently(worker, n_threads * 2)
208208

209209
@staticmethod
210210
def is_sorted_ascending(lst):

0 commit comments

Comments
 (0)