| 538 | |
| 539 | |
| 540 | def run_multithreaded(closure, max_workers, arguments=None, pass_barrier=False): |
| 541 | with ThreadPoolExecutor(max_workers=max_workers) as tpe: |
| 542 | if arguments is None: |
| 543 | arguments = [] |
| 544 | else: |
| 545 | arguments = list(arguments) |
| 546 | |
| 547 | if pass_barrier: |
| 548 | barrier = threading.Barrier(max_workers) |
| 549 | arguments.append(barrier) |
| 550 | |
| 551 | try: |
| 552 | futures = [] |
| 553 | for _ in range(max_workers): |
| 554 | futures.append(tpe.submit(closure, *arguments)) # noqa: PERF401 |
| 555 | except RuntimeError as e: |
| 556 | import pytest |
| 557 | |
| 558 | pytest.skip( |
| 559 | f"Spawning {max_workers} threads failed with " |
| 560 | f"error {e!r} (likely due to resource limits on the " |
| 561 | "system running the tests)" |
| 562 | ) |
| 563 | finally: |
| 564 | if len(futures) < max_workers and pass_barrier: |
| 565 | barrier.abort() |
| 566 | for f in futures: |
| 567 | f.result() |
| 568 | |
| 569 | |
| 570 | __all__ = [ |