简体   繁体   中英

tenacity retrying with exception handler

Can tenacity handle this or should I implement retry wrapper myself if I need to catch exception do a callback and get back to next try?

send → fetch error → if recoverable → run callback → try send again

When I use a simple case with this code, next try never happened:

class A:
    a = 0
 
    @retry(stop=stop_after_attempt(7))
    def never_give_up_never_surrender(cls):
        try:
            1/cls.a
            print('possibly wrong')
        except ZeroDivisionError:
            cls.a+=1
            print('next try')
        else:
            print('done')

There is a way:

def error_callback(retry_state):
   # error handler
   if isinstance(retry_state._exception(ZeroDivisionError)):
     print("Can't handle it")
   else:
     print("Handled")


@retry(retry_error_callback=error_callback):
def zero_division():
  a = 1 
  b = 0
  a/b

It's not obviously described in documentation but in API.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM