timeout

Class summary

RunableProcessing(func, *args, **kwargs)
Attributes:
TimeoutException

Function summary

_longcos(x[, wait]) calc cos(x) after waiting wait seconds.
timeout(seconds[, force_kill])

Module listing

Code to timeout with processes.

References

Code in this module comes from an activestate code recipe [Rdeea4949bfb9-1]. For an asynchronous solution see the active activestate code recipe [Rdeea4949bfb9-2].

[Rdeea4949bfb9-1]timeout decorator (with multiprocessing) (Python recipe) synchronous
[Rdeea4949bfb9-2]Timeout Any Function (Python recipe) asynchronous

Examples

>>> timed_longcos = timeout(2)(_longcos)
>>> timed_longcos(1, 0)
0.5403...
>>> timed_longcos(1, 2) 
Traceback (most recent call last):
...
TimeoutException: timed out after 2 seconds

Notes

The following examples from the original activestate code recipe demonstrate how to use timeout as a decorator. They don’t seem to work for me as the functions must be defined in __main__ to be pickled. you get the idea though.

@timeout(.5)
def sleep(x):
    print "ABOUT TO SLEEP {0} SECONDS".format(x)
    time.sleep(x)
    return x

sleep(1)
Traceback (most recent call last):
   ...
TimeoutException: timed out after 0 seconds

sleep(.2)
0.2

@timeout(.5)
def exc():
    raise Exception('Houston we have problems!')

exc()
Traceback (most recent call last):
   ...
Exception: Houston we have problems!
class geotecha.inputoutput.timeout.RunableProcessing(func, *args, **kwargs)[source]

Bases: multiprocessing.context.Process

Attributes:
authkey
daemon

Return whether process is a daemon

exitcode

Return exit code of process or None if it has yet to stop

ident

Return identifier (PID) of process or None if it has yet to start

name
pid

Return identifier (PID) of process or None if it has yet to start

sentinel

Return a file descriptor (Unix) or handle (Windows) suitable for waiting for process termination.

Methods

is_alive() Return whether process is alive
join([timeout]) Wait until child process terminates
run() Method to be run in sub-process; can be overridden in sub-class
start() Start child process
terminate() Terminate process; sends SIGTERM signal or uses TerminateProcess()
done  
result  
run_func  
done()[source]
result()[source]
run_func(func, *args, **kwargs)[source]
exception geotecha.inputoutput.timeout.TimeoutException[source]

Bases: Exception

geotecha.inputoutput.timeout.timeout(seconds, force_kill=True)[source]