It's a type of Planche
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1234567891011121314151617181920212223242526272829
  1. from functools import wraps
  2. import errno
  3. import os
  4. import signal
  5. class TimeoutError(Exception):
  6. """Generic Timeout Error."""
  7. def timeout(seconds=10, error_message=os.strerror(errno.ETIME)):
  8. """Timeout decorator to wrap around another callable.
  9. https://stackoverflow.com/questions/2281850/timeout-function-if-it-takes-too-long-to-finish
  10. """
  11. def decorator(func):
  12. def _handle_timeout(signum, frame):
  13. raise TimeoutError(error_message)
  14. def wrapper(*args, **kwargs):
  15. signal.signal(signal.SIGALRM, _handle_timeout)
  16. signal.alarm(seconds)
  17. try:
  18. result = func(*args, **kwargs)
  19. finally:
  20. signal.alarm(0)
  21. return result
  22. return wraps(func)(wrapper)
  23. return decorator