Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def wrapper(*args, **kwargs):
res = [TimeoutError('artificial timeout error')]
def new_func():
try:
res[0] = func(*args, **kwargs)
except Exception as e:
res[0] = e
t = Thread(target=new_func)
t.daemon = True
try:
t.start()
t.join(seconds_before_timeout)
except Exception as e:
raise e
ret = res[0]
if isinstance(ret, TimeoutError):
raise ret
@functools.wraps(func)
def wrapper(*args, **kwargs):
res = [TimeoutError('artificial timeout error')]
def new_func():
try:
res[0] = func(*args, **kwargs)
except Exception as e:
res[0] = e
t = multiprocessing.Process(target=new_func)
t.daemon = True
try:
t.start()
t.join(seconds_before_timeout)
except Exception as e:
raise e
ret = res[0]
if isinstance(ret, BaseException):
t.terminate()
res = [TimeoutError('artificial timeout error')]
def new_func():
try:
res[0] = func(*args, **kwargs)
except Exception as e:
res[0] = e
t = Thread(target=new_func)
t.daemon = True
try:
t.start()
t.join(seconds_before_timeout)
except Exception as e:
raise e
ret = res[0]
if isinstance(ret, TimeoutError):
raise ret
return ret
return wrapper