It's a type of Planche
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

30 rindas
808B

  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