超时操作

pip install func_timeout

from func_timeout import func_timeout, FunctionTimedOut
import time

def doit(arg1, arg2):
    time.sleep(3)
    print(arg1, arg2)

try:
    doitReturnValue = func_timeout(2, doit, args=('arg1', 'arg2'))
except FunctionTimedOut:
    print("doit(‘arg1’, ‘arg2’) could not complete within 5 seconds and was terminated.\n")
except Exception as e:
    print('exception')
# Handle any exceptions that doit might raise here

你可能感兴趣的:(超时操作)