Skip to content

Python library to safely execute anything without fears to stuck at TASK_UNINTERRUPTIBLE ('D') state

License

Notifications You must be signed in to change notification settings

amarao/thread_timeout

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

30 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Library to safely execute code without fear of the TASK_UNINTERRUPTIBLE state

thread_timeout allows to run given piece of python code safely regardless of TASK_UNINTERRUPTIBLE issues.

It provides a single decorator with a timeout for a function call.

Example:

import thread_timeout

@thread_timeout(10, kill=False)
def NFS_read(path):
    file(path, 'r').read()

try:
    print("Result: %s" % NFS_read('broken_nfs/file'))
except ExecTimeoutException:
    print ("NFS seems to be hung")

thread_timeout works by running specified function in a separate thread and waiting for timeout (or termination) of the thread. It returns to functions return value or reraise exception.

If thread does not finish before timeout, thread_timeout may try to terminate thread according to kill_wait value (see below).

thread_timeout(timeout, kill=True, kill_wait=0.1)

timeout - seconds, floating, how long to wait thread. kill - if True (default) attempt to terminate thread with function kill_wait - how long to wait after killing before reporting an unresponsive thread

THREAD KILLING

Thread killing implemented on python level: it will terminate python code, but will not terminate any IO operations or subprocess calls.

Exceptions:

ExecTimeoutException - function did not finish on time, timeout (base class for all following exceptions) KilledExecTimeoutException - there was a timeout and thread with function was killed successfully FailedKillExecTimeoutException - there was a timeout and kill attempt but the thread refuses to die NotKillExecTimeoutException - there was a timeout and there was no attempt to kill thread

About

Python library to safely execute anything without fears to stuck at TASK_UNINTERRUPTIBLE ('D') state

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages