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 support for an async defined retry and callback_error_retry #289

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
---
prelude: >
Example use cases:
- if we get logged out from the server
- if authentication tokens expire
We want to be able to automatically refresh our session by calling a specific
function. This function can be asynchronously defined.
features:
- |
Asynchronous defined retry callbacks:
- retry
- retry_error_callback
issues:
- |
#249

Comment on lines +2 to +16
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
prelude: >
Example use cases:
- if we get logged out from the server
- if authentication tokens expire
We want to be able to automatically refresh our session by calling a specific
function. This function can be asynchronously defined.
features:
- |
Asynchronous defined retry callbacks:
- retry
- retry_error_callback
issues:
- |
#249
features:
- Allow to define asynchronous defined retry callbacks by using `retry` or `retry_error_callback`

60 changes: 57 additions & 3 deletions tenacity/_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,17 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
try:
from inspect import iscoroutinefunction
except ImportError:
iscoroutinefunction = None

import sys
from asyncio import sleep

from tenacity import AttemptManager
import six

from tenacity import AttemptManager, RetryAction, TryAgain
from tenacity import BaseRetrying
from tenacity import DoAttempt
from tenacity import DoSleep
Expand All @@ -30,12 +37,58 @@ def __init__(self, sleep=sleep, **kwargs):
super(AsyncRetrying, self).__init__(**kwargs)
self.sleep = sleep

async def iter(self, retry_state): # noqa
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why the noqa here? 🤔

I'm ok on the idea, but the implementation has too much copy pasting from the parent iter. It needs better refactoring for applying DRY.

fut = retry_state.outcome
if fut is None:
if self.before is not None:
self.before(retry_state)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add async support for before and after callbacks?

return DoAttempt()

is_explicit_retry = retry_state.outcome.failed and isinstance(
retry_state.outcome.exception(), TryAgain
)
if iscoroutinefunction(self.retry):
should_retry = await self.retry(retry_state=retry_state)
else:
should_retry = self.retry(retry_state=retry_state)
if not (is_explicit_retry or should_retry):
return fut.result()

if self.after is not None:
self.after(retry_state=retry_state)

self.statistics["delay_since_first_attempt"] = retry_state.seconds_since_start
if self.stop(retry_state=retry_state):
if self.retry_error_callback:
if iscoroutinefunction(self.retry_error_callback):
return await self.retry_error_callback(retry_state=retry_state)
else:
return self.retry_error_callback(retry_state=retry_state)
retry_exc = self.retry_error_cls(fut)
if self.reraise:
raise retry_exc.reraise()
six.raise_from(retry_exc, fut.exception())

if self.wait:
iteration_sleep = self.wait(retry_state=retry_state)
else:
iteration_sleep = 0.0
retry_state.next_action = RetryAction(iteration_sleep)
retry_state.idle_for += iteration_sleep
self.statistics["idle_for"] += iteration_sleep
self.statistics["attempt_number"] += 1

if self.before_sleep is not None:
self.before_sleep(retry_state=retry_state)

return DoSleep(iteration_sleep)

async def __call__(self, fn, *args, **kwargs):
self.begin(fn)

retry_state = RetryCallState(retry_object=self, fn=fn, args=args, kwargs=kwargs)
while True:
do = self.iter(retry_state=retry_state)
do = await self.iter(retry_state=retry_state)
if isinstance(do, DoAttempt):
try:
result = await fn(*args, **kwargs)
Expand All @@ -56,7 +109,7 @@ def __aiter__(self):

async def __anext__(self):
while True:
do = self.iter(retry_state=self._retry_state)
do = await self.iter(retry_state=self._retry_state)
if do is None:
raise StopAsyncIteration
elif isinstance(do, DoAttempt):
Expand All @@ -69,6 +122,7 @@ async def __anext__(self):

def wraps(self, fn):
fn = super().wraps(fn)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unrelated 😁

# Ensure wrapper is recognized as a coroutine function.

async def async_wrapped(*args, **kwargs):
Expand Down
43 changes: 43 additions & 0 deletions tenacity/tests/test_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,49 @@ def after(retry_state):
assert len(set(things)) == 1
assert list(attempt_nos2) == [1, 2, 3]

@asynctest
async def test_async_retry(self):
attempts = []

async def async_retry(retry_state):
if retry_state.outcome.failed:
attempts.append((retry_state.outcome, retry_state.attempt_number))
return True
else:
attempts.append((retry_state.outcome, retry_state.attempt_number))
return False

thing = NoIOErrorAfterCount(2)

await _retryable_coroutine.retry_with(retry=async_retry)(thing)

things, attempt_numbers = zip(*attempts)
assert len(attempts) == 3

for thing in things[:-1]:
with pytest.raises(IOError):
thing.result()

assert things[-1].result() is True

@asynctest
async def test_async_callback_error_retry(self):
async def async_return_text(retry_state):
await asyncio.sleep(0.00001)

return "Calling %s keeps raising errors after %s attempts" % (
retry_state.fn.__name__,
retry_state.attempt_number,
)

thing = NoIOErrorAfterCount(3)

result = await _retryable_coroutine_with_2_attempts.retry_with(
retry_error_callback=async_return_text
)(thing)
message = "Calling _retryable_coroutine_with_2_attempts keeps raising errors after 2 attempts"
assert result == message


class TestContextManager(unittest.TestCase):
@asynctest
Expand Down