Skip to content

Commit

Permalink
Conditional Timeout() by OS (disable on Windows) (#7013)
Browse files Browse the repository at this point in the history
* Conditional `Timeout()` by OS (disable on Windows)

* Update general.py
  • Loading branch information
glenn-jocher authored Mar 16, 2022
1 parent c09fb2a commit 3f634d4
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions utils/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,13 +123,15 @@ def _timeout_handler(self, signum, frame):
raise TimeoutError(self.timeout_message)

def __enter__(self):
signal.signal(signal.SIGALRM, self._timeout_handler) # Set handler for SIGALRM
signal.alarm(self.seconds) # start countdown for SIGALRM to be raised
if platform.system() != 'Windows': # not supported on Windows
signal.signal(signal.SIGALRM, self._timeout_handler) # Set handler for SIGALRM
signal.alarm(self.seconds) # start countdown for SIGALRM to be raised

def __exit__(self, exc_type, exc_val, exc_tb):
signal.alarm(0) # Cancel SIGALRM if it's scheduled
if self.suppress and exc_type is TimeoutError: # Suppress TimeoutError
return True
if platform.system() != 'Windows':
signal.alarm(0) # Cancel SIGALRM if it's scheduled
if self.suppress and exc_type is TimeoutError: # Suppress TimeoutError
return True


class WorkingDirectory(contextlib.ContextDecorator):
Expand Down

0 comments on commit 3f634d4

Please sign in to comment.