You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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
Before any attempt of calling the function (before action)
After a call that failed (including after the last attempt) (after action)
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:
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.
Activity
asqui commentedon Sep 1, 2022
Looking at Before and After Retry, and Logging it is possible to execute an action:
beforeaction)afteraction)before_sleepaction)None of these matches your use case exactly, but 1 (the
beforeaction) 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:
after_sleepactionUnfortunately this is not currently supported, but I'm sure a PR to add this would be considered.
ArcangeloGarbellini commentedon Sep 5, 2022
Ok, I'll add a PR request. Thanks
vinjana commentedon Jun 26, 2023
Seems like a duplicate of #298.
I don't find a corresponding PR.
jd commentedon Feb 23, 2026
Use the
before_sleepcallback to run a function between retries:Closing as a usage question (duplicate of #298).
docs: add common patterns section for recurring FAQ topics
docs: add common patterns section for recurring FAQ topics (#602)