spacepy.toolbox.timeout_check_call

spacepy.toolbox.timeout_check_call(timeout, *args, **kwargs)[source]

Call a subprocess with a timeout.

Like subprocess.check_call(), but will terminate the process and raise TimeoutError if it runs for too long.

This will only terminate the single process started; any child processes will remain running (this has implications for, say, spawing shells.)

Parameters:

timeout : float

Timeout, in seconds. Fractions are acceptable but the resolution is of order 100ms.

args : sequence

Arguments passed through to subprocess.Popen

kwargs : dict

keyword arguments to pass to subprocess.Popen

Returns:

out : int

0 on successful completion

Raises:

TimeoutError : If subprocess does not return in timeout seconds.

CalledProcessError : if command has non-zero exit status

Examples

>>> import spacepy.toolbox as tb
>>> tb.timeout_check_call(1, 'sleep 30', shell=True) #raises TimeoutError