(inputs: List[Any], num_workers: int, pbar = None)
| 222 | |
| 223 | |
| 224 | def multithead_execute(inputs: List[Any], num_workers: int, pbar = None): |
| 225 | from concurrent.futures import ThreadPoolExecutor |
| 226 | from contextlib import nullcontext |
| 227 | from tqdm import tqdm |
| 228 | |
| 229 | if pbar is not None: |
| 230 | pbar.total = len(inputs) if hasattr(inputs, '__len__') else None |
| 231 | else: |
| 232 | pbar = tqdm(total=len(inputs) if hasattr(inputs, '__len__') else None) |
| 233 | |
| 234 | def decorator(fn: Callable): |
| 235 | with ( |
| 236 | ThreadPoolExecutor(max_workers=num_workers) as executor, |
| 237 | pbar |
| 238 | ): |
| 239 | pbar.refresh() |
| 240 | @catch_exception |
| 241 | @suppress_traceback |
| 242 | def _fn(input): |
| 243 | ret = fn(input) |
| 244 | pbar.update() |
| 245 | return ret |
| 246 | executor.map(_fn, inputs) |
| 247 | executor.shutdown(wait=True) |
| 248 | |
| 249 | return decorator |
| 250 | |
| 251 | |
| 252 | def suppress_traceback(fn): |
nothing calls this directly
no outgoing calls
no test coverage detected