tenacity icon indicating copy to clipboard operation
tenacity copied to clipboard

Call another function before retry

Open ArcangeloGarbellini opened this issue 3 years ago • 3 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

ArcangeloGarbellini avatar Sep 01 '22 16:09 ArcangeloGarbellini

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.

asqui avatar Sep 01 '22 18:09 asqui

Ok, I'll add a PR request. Thanks

ArcangeloGarbellini avatar Sep 05 '22 13:09 ArcangeloGarbellini

Seems like a duplicate of https://github.com/jd/tenacity/issues/298.

I don't find a corresponding PR.

vinjana avatar Jun 26 '23 09:06 vinjana