jd / tenacity

Retrying library for Python
http://tenacity.readthedocs.io
Apache License 2.0
6.82k stars 283 forks source link

Add support for async strategies and callbacks #433

Closed hasier closed 10 months ago

hasier commented 10 months ago

Fixes #249 Follows up on #289 #363

I took a stab at DRYing the iter() function in order to support async callbacks, and once those were possible, I thought it would also make sense to allow for async strategies.

The approach I have taken is to build a list of actions that iter() needs to go through, where each action is just a piece of the whole current logic, and each step can further extend the list of actions. 3 pieces have been taken out to redefine in specific Retrying implementations, i.e. asyncio. This way we can make sure we use coroutines for those calls, as well as dynamically wrapping each function as a coroutine, allowing for both sync and async strategies and callbacks.

I tried to make somewhat meaningful commits, it should be easier to review commit-by-commit.

I appreciate the new approach looks quite odd and is not very intuitive to follow, happy to take some feedback and improve it if anyone has some other ideas.

mergify[bot] commented 10 months ago

⚠️ No release notes detected. Please make sure to use reno to add a changelog entry.

jd commented 10 months ago

Is there a way to split that in smaller changes? It's a bit large to review as is, even if it looks great

hasier commented 10 months ago

Is there a way to split that in smaller changes? It's a bit large to review as is, even if it looks great

@jd fair point. I have split it into 3 PRs, hopefully this works for you.

  1. https://github.com/jd/tenacity/pull/434
  2. https://github.com/hasier/tenacity/pull/1 stacked on the previous one, but in my repo. Let me know if you prefer I did it against your main instead.
  3. https://github.com/hasier/tenacity/pull/2 stacked on the previous one, but in my repo too. Let me know if you prefer I did it against your main instead.