Sakthivel
Sakthivel

Reputation: 61

Python : how to use retry() on a function , it should retry until that function returns True

I'm trying to use retry but it's not retrying.

retry(stop_max_attempt_number=8,
      wait_fixed=15000)(check_type(var1,type))

check_type : this function will return either true or false. It should retry until this function returns true. please help to resolve this.

Upvotes: 1

Views: 1533

Answers (1)

Edward Aymerich
Edward Aymerich

Reputation: 36

I assume you're asking about retry function from retrying package. retry() will retry your function if it raises and exception, or if an assert fails, so you can wrap your function into another function like this:

def wrap_function(var1, type):
    return_value = check_type(var1, type)
    assert return_value is True

caller = retry(stop_max_attempt_number=8, wait_fixed=15000)(
    wrap_function)
caller(var1, type)

Also note that retry() must receive the function to execute, and it returns a function that you can call. So you must drop the parenthesis and arguments when you pass the function to retry() (otherwise you will be passing True or False) and you must save the returned function in order to call it later (I used variable caller in my example).

Upvotes: 2

Related Questions