| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
| 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) |
| 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'] |
| Back | FazBrowse Home | New Git URL |
Uh oh!
There was an error while loading. Please reload this page.