-
-
Notifications
You must be signed in to change notification settings - Fork 30.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
6 changed files
with
215 additions
and
56 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import contextlib | ||
import time | ||
|
||
|
||
class DeadlineExceeded(Exception): | ||
pass | ||
|
||
|
||
class TimedContext(contextlib.ContextDecorator): | ||
""" | ||
A context that will raise DeadlineExceeded if the | ||
max duration is reached during the execution. | ||
>>> TimedContext(1)(time.sleep)(.1) | ||
>>> TimedContext(0)(time.sleep)(.1) | ||
Traceback (most recent call last): | ||
... | ||
tests._context.DeadlineExceeded: (..., 0) | ||
""" | ||
|
||
def __init__(self, max_duration: int): | ||
self.max_duration = max_duration | ||
|
||
def __enter__(self): | ||
self.start = time.monotonic() | ||
|
||
def __exit__(self, *err): | ||
duration = time.monotonic() - self.start | ||
if duration > self.max_duration: | ||
raise DeadlineExceeded(duration, self.max_duration) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
try: | ||
from func_timeout import func_set_timeout as set_timeout | ||
except ImportError: # pragma: no cover | ||
# provide a fallback that doesn't actually time out | ||
from ._context import TimedContext as set_timeout | ||
|
||
|
||
__all__ = ['set_timeout'] |
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
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
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
Oops, something went wrong.