多线程并发提交,返回 (results, errors)。
(engine, payload: dict, n_threads: int, per_thread: int)
| 105 | |
| 106 | |
| 107 | def _submit_concurrent(engine, payload: dict, n_threads: int, per_thread: int): |
| 108 | """多线程并发提交,返回 (results, errors)。""" |
| 109 | results: List[dict] = [] |
| 110 | errors: List[Exception] = [] |
| 111 | lock = threading.Lock() |
| 112 | barrier = threading.Barrier(n_threads) |
| 113 | |
| 114 | def worker(): |
| 115 | barrier.wait() |
| 116 | for _ in range(per_thread): |
| 117 | try: |
| 118 | r = engine.submit_request(dict(payload)) |
| 119 | with lock: |
| 120 | results.append(r) |
| 121 | except Exception as exc: |
| 122 | with lock: |
| 123 | errors.append(exc) |
| 124 | |
| 125 | threads = [threading.Thread(target=worker) for _ in range(n_threads)] |
| 126 | for t in threads: |
| 127 | t.start() |
| 128 | for t in threads: |
| 129 | t.join(timeout=30) |
| 130 | return results, errors |
| 131 | |
| 132 | |
| 133 | def _avg_utilization(engine) -> float: |
no outgoing calls
no test coverage detected