Skip to content

Instantly share code, notes, and snippets.

@shar1z
Created August 1, 2022 10:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shar1z/24b65853c13cb6b92fc97e04173b652e to your computer and use it in GitHub Desktop.
Save shar1z/24b65853c13cb6b92fc97e04173b652e to your computer and use it in GitHub Desktop.
@contextlib.contextmanager
def time_limit(seconds):
def raise_timeout(signum, frame):
raise TimeoutError('Timed out!')
signal.signal(signal.SIGALRM, raise_timeout)
signal.alarm(seconds)
yield
signal.alarm(0) # Disable the alarm
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment