Skip to content
New issue

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

Add RetryingContext class (B) #60

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions tenacity/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,32 @@ def call(self, fn, *args, **kwargs):

__call__ = call

class RetryingContext(object):
"""A classic context manager is NOT able to suspend execution in its enter and exit methods."""

def __init__(self, f, *args, **kwargs):
self.f = f
self.args = args
self.kwargs = kwargs

def __enter__(self):
f = self.f
if asyncio and asyncio.iscoroutinefunction(f):
r = AsyncRetrying(*self.args, **self.kwargs)
else:
r = Retrying(*self.args, **self.kwargs)

@six.wraps(f)
def wrapped_fn(*args, **kw):
return r.call(f, *args, **kw)

wrapped_f.retry = r
return wrapped_f

def __exit__(self, exc_type, exc_val, exc_tb):
# If we returned True here, any exception inside the with block would be suppressed!
return False


class Future(futures.Future):
"""Encapsulates a (future or past) attempted call to a target function."""
Expand Down