jd / tenacity

Retrying library for Python

Home Page:http://tenacity.readthedocs.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Call another function before retry

ArcangeloGarbellini opened this issue · comments

Hello, is it possible to call another function before retrying?
I have a try and except block, and I know that if it fails it is because of a connection issue, so before the next attempt I would like to run another function that reinitialise the connection. Is that possible with the tenacity package?
Thank you

Looking at Before and After Retry, and Logging it is possible to execute an action:

  1. Before any attempt of calling the function (before action)
  2. After a call that failed (including after the last attempt) (after action)
  3. After a failure that is going to be retried (but before the sleep) (before_sleep action)

None of these matches your use case exactly, but 1 (the before action) sounds like it might be the closest.

I guess you can either restructure your code to have the connection for the first attempt also established in this action, or to no-op the action on the first attempt.

Sounds like the enhancement that would meet your needs of executing an action before any retry would be:

  1. Ability to specify an after_sleep action

Unfortunately this is not currently supported, but I'm sure a PR to add this would be considered.

Ok, I'll add a PR request. Thanks

Seems like a duplicate of #298.

I don't find a corresponding PR.