def try_with_timeout(target, args, timeout, pause):
"""
Attempt an operation as long as it fails up to the given timeout.
:param target: The callable
:param args: The arguments to pass
:param timeout: the timeout
:param pause: how long to pause between each failed invocation
:return: either the result from successfully invoking the callable
raises the exception thrown by the callable on timeout.
"""
end = time.perf_counter()+timeout
result, exception = try_invoke(target, args)
while exception is not None and time.perf_counter()<end:
time.sleep(pause)
result, exception = try_invoke(target, args)
if exception is not None:
raise exception
return result
评论列表
文章目录