To check the current time of retry in a function decorated with the retrying library in Python, you can use the @retry decorator and pass a function as the wait_func argument that returns the current time.

Here's an example:

import time
import random
import retrying

@retrying.retry(wait_fixed=1000, wait_func=lambda x: time.time(), stop_max_attempt_number=5)
def my_function():
    print('Running my_function...')
    # simulate a random failure
    if random.random() < 0.5:
        print('Failed!')
        raise Exception('Something went wrong')
    else:
        print('Success!')

my_function()

In this example, we're using the @retry decorator to retry the my_function function up to 5 times, with a fixed wait time of 1 second between retries (wait_fixed=1000). We're also using a lambda function as the wait_func argument, which simply returns the current time (lambda x: time.time()).

When my_function is called, the @retry decorator will automatically handle the retries and wait times, and the wait_func lambda function will be called each time to get the current time. You can use the time.time() function in the lambda function to get the current time in seconds since the epoch.

Python Retry Library: How to Get Current Retry Time in Decorated Function

原文地址: https://www.cveoy.top/t/topic/nDJm 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录