To count the retry statistics of a function decorated by the retrying library in Python, you can use the retrying.statistics module. This module provides a Statistics class that keeps track of the number of retries, the number of successful calls, and the total elapsed time.

Here's an example:

import retrying
from retrying.statistics import Statistics

@retrying.retry(wait_fixed=1000, stop_max_attempt_number=3)
def my_function():
    print("Trying...")
    raise Exception("Oops")

stats = Statistics()

try:
    my_function()
except Exception:
    pass

print("Retries:", stats.num_retries)
print("Successful calls:", stats.num_successes)
print("Total elapsed time:", stats.total_elapsed_time)

In this example, we have a function my_function decorated with the retrying library. It will retry up to 3 times with a fixed wait time of 1 second between retries.

We create a Statistics object before calling the function and then catch any exceptions that are raised. After the function has completed (either successfully or after all retries have been exhausted), we print out the retry statistics using the num_retries, num_successes, and total_elapsed_time properties of the Statistics object.

This will output something like:

Trying...
Trying...
Trying...
Retries: 2
Successful calls: 0
Total elapsed time: 3002.116894006729
``
how to count retry statistics of a function decorated by retrying library in python

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

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