2

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)

Sadra
  • 2,480
  • 2
  • 20
  • 32
  • try tenacity(https://github.com/jd/tenacity). it may have all functionalities you need. – 정도유 Mar 28 '22 at 05:37
  • If your question is how to use a library function, then you need to defer to the official documentation for said library. – ddejohn Mar 28 '22 at 17:08
  • "Can anyone let me know how to do this? meaning using one of these maintaining libraries to implement handle this case." You are asking how to use a library. If you have a *specific* question about implementation, then update your post with your code attempt and what exactly the problem is with your attempt. Right now, your question is vaguely "how do I use one of these libraries to implement this feature", which is off-topic for Stack Overflow. – ddejohn Mar 28 '22 at 17:17
  • The link in *your* post to the `backoff` library has plenty of examples and clear documentation for how to implement various backoff/retry processes. If you have a specific problem with one of the approaches using the library *you have requested* then you need to post it. You claim your question is "not a straightforward documentation question or library 'how to use'" but you have not indicated otherwise yet through any edits or clarifying comments -- you are just complaining about downvotes. – ddejohn Mar 28 '22 at 17:28

1 Answers1

0

the iterator can help this.

@tenacity.retry
def try_to_extract(value_iter):
    curr_val = next(value_iter)
    print(curr_val)
    try:
        return extract(arg1,arg2,val=curr_val)
    except Exception:
        raise

curr_val = 40
try_to_extract(itertools.count(curr_val, -1))
정도유
  • 559
  • 4
  • 6