Skip to content

Instantly share code, notes, and snippets.

@lmatt-bit
Created October 17, 2019 03:28
Show Gist options
  • Save lmatt-bit/da6cf9aa481c4bf31103d8d51a47e6d3 to your computer and use it in GitHub Desktop.
Save lmatt-bit/da6cf9aa481c4bf31103d8d51a47e6d3 to your computer and use it in GitHub Desktop.
python decorator example
def timeout_function(timeout, force_clean_func):
def decorator_timeout_function(func):
@functools.wraps(func)
def run_func_in_thread(*args, **kwargs):
thread = threading.Thread(target=func, args=args, kwargs=kwargs)
thread.start()
thread.join(timeout)
if thread.is_alive():
print("timeout, begin to call force_clean_func")
force_clean_func()
raise TimeoutException()
return run_func_in_thread
return decorator_timeout_function
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment