Tried to write a process-based timeout (sync) on the cheap, like this:
from concurrent.futures import ProcessPoolExecutor
def call_with_timeout(func, *args, timeout=3):
with ProcessPoolExecutor(max_workers=1) as pool:
future = pool.submit(func, *args)
result = future.result(timeout=timeout)
But it seems the timeout
argument passed to future.result doesn't really work as advertised.
>>> t0 = time.time()
... call_with_timeout(time.sleep, 2, timeout=3)
... delta = time.time() - t0
... print('wall time:', delta)
wall time: 2.016767978668213
OK.
>>> t0 = time.time()
... call_with_timeout(time.sleep, 5, timeout=3)
... delta = time.time() - t0
... print('wall time:', delta)
# TimeoutError
Not OK - unblocked after 5 seconds, not 3 seconds.
Related questions show how to do this with thread pools, or with signal. How to timeout a process submitted to a pool after n seconds, without using any _private API of multiprocessing? Hard kill is fine, no need to request a clean shutdown.