Created
October 17, 2019 03:28
-
-
Save lmatt-bit/da6cf9aa481c4bf31103d8d51a47e6d3 to your computer and use it in GitHub Desktop.
python decorator example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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