Using Retrying at run time, how can I actually retry without raising exception ?
Hi,
I'm currently trying to implement Tenacity in a code that use a low-bandwidth communication modem to send data. Here is the pseudo-code :
from tenacity import retry,stop_after_delay,Retrying,stop_after_attempt
import time
import random
class Sender:
def __init__(self) -> None:
self.sending_timeout = 10
self.sending_retries = 5
def get_sending_timeout(self):
return self.sending_timeout
def sending_emulation(self):
print("sending...")
time.sleep(5)
fail = random.randint(0,5)
if not fail:
print("Success !")
else:
raise Exception("Could not send!")
def sending_emulation_timeout(self):
retryier = Retrying(stop=stop_after_delay(self.sending_timeout),reraise=True)
retryier(self.sending_emulation(),"test")
if __name__ == "__main__":
s = Sender()
s.sending_emulation_timeout()
The amount of retries, and the timeout are specified in a configuration file which are then loaded as attributes. I'd like to use these attributes to modify Retry behavior at run time. My problem in this example, is that sending_emulation_timeout() doesn't actually retries but just re raise the exception. I tried using reraise=False as well, but no luck. How can I make it retry according to the timeout ?
As stated here https://github.com/jd/tenacity#changing-arguments-at-run-time. There should be no method call inside retrayer Your code should be:
def sending_emulation_timeout(self):
retryier = Retrying(stop=stop_after_delay(self.sending_timeout), reraise=True)
retryier(self.sending_emulation)
The Reraise flag affects raise the original error or a specific Tenacity error will be returned