how do I retry a command only for a specific exception / error
Steven D'Aprano
steve+comp.lang.python at pearwood.info
Fri Mar 16 01:51:34 EDT 2018
On Fri, 16 Mar 2018 11:04:22 +0530, Ganesh Pal wrote:
> All that I am trying to do here is write a generic function that will
> re-retry
> the command few more times before failing the test
Something like this should do it. It gives up immediately on fatal errors
and tries again on temporary ones. (You have to specify what you consider
fatal or temporary, of course.) It uses exponential backup to wait longer
and longer each time you fail, before eventually giving up.
delay = 2
for attempts in range(max_attempts):
try:
command()
except PermanentFailureError:
raise
except TemporaryFailureError:
sleep(delay)
delay *= 2
else:
break
else:
raise RuntimeError("too many attempts")
--
Steve
More information about the Python-list
mailing list