(fn: Callable[..., T], timeout_s: float)
| 28 | |
| 29 | |
| 30 | def with_timeout(fn: Callable[..., T], timeout_s: float) -> T: |
| 31 | global TIMEOUT_POOL |
| 32 | |
| 33 | if TIMEOUT_POOL is None: |
| 34 | # it needs to be more than 1 to avoid deadlocks when with_timeout is nested |
| 35 | TIMEOUT_POOL = ThreadPool(8) |
| 36 | atexit.register(TIMEOUT_POOL.close) |
| 37 | |
| 38 | future = TIMEOUT_POOL.apply_async(fn) |
| 39 | try: |
| 40 | return future.get(timeout=timeout_s) |
| 41 | except multiprocessing.context.TimeoutError: |
| 42 | raise TimeoutException() |
| 43 | |
| 44 | |
| 45 | class Timings: |
no test coverage detected