I am trying to implement a retry ability whenever a function fails with an index error. I started with this:
I know the reason for failure is passing high value to curr_val, but setting high values will generate better output
#these lines are inside another for loop
curr_val=40
while True:
try:
ret1, ret2 = extract(arg1,arg2,val=curr_val)
except IndexError:
curr_val -=5
continue
break
##process ret1
According to this answer, it is possible to use the decorator (like tenacity) to handle such cases, supporting any kind of exception.
my current try with tenacity is as follows:
curr_val = 5
@tenacity.retry(wait=tenacity.wait_fixed(1))
def try_to_extract():
try:
return extract(arg1,arg2,val=curr_val)
except Exception:
curr_val -=1
raise
However, it does not have access to the outside variables and keeps raising exception, without changing curr_val
Can anyone let me know how to handle this? meaning using curr_val
inside retry and handle this case. (retrying with another argument(decremented curr_val), in case of failure or timeout)