Execute a function (or method) with a timeout.
Call the function (or method) target, with arguments args and keyword arguments kwargs. Normally return the return value from target, but if target takes more than timeout seconds to execute, raises TimeoutError.
Note
This is, at best, a blunt instrument. Exceptions from target may not propagate properly (tracebacks will be hard to follow.) The function which failed to time out may continue to execute until the interpreter exits; trapping the TimeoutError and continuing normally is not recommended.
Parameters: | timeout : float
target : callable
args : sequence
kwargs : dict
|
---|---|
Returns: | out : :
|
Raises: | TimeoutError : If target does not return in timeout seconds. |
Examples
>>> import spacepy.toolbox as tb
>>> import time
>>> def time_me_out():
... time.sleep(5)
>>> tb.do_with_timeout(0.5, time_me_out) #raises TimeoutError