Run the worker function concurrently in multiple threads.
(worker_func, nthreads, args=(), kwargs={})
| 250 | |
| 251 | |
| 252 | def run_concurrently(worker_func, nthreads, args=(), kwargs={}): |
| 253 | """ |
| 254 | Run the worker function concurrently in multiple threads. |
| 255 | """ |
| 256 | barrier = threading.Barrier(nthreads) |
| 257 | |
| 258 | def wrapper_func(*args, **kwargs): |
| 259 | # Wait for all threads to reach this point before proceeding. |
| 260 | barrier.wait() |
| 261 | worker_func(*args, **kwargs) |
| 262 | |
| 263 | with catch_threading_exception() as cm: |
| 264 | workers = [ |
| 265 | threading.Thread(target=wrapper_func, args=args, kwargs=kwargs) |
| 266 | for _ in range(nthreads) |
| 267 | ] |
| 268 | with start_threads(workers): |
| 269 | pass |
| 270 | |
| 271 | # If a worker thread raises an exception, re-raise it. |
| 272 | if cm.exc_value is not None: |
| 273 | raise cm.exc_value |
nothing calls this directly
no test coverage detected