Tenacity is an Apache 2.0 licensed general-purpose retrying library, written in Python, to simplify the task of adding retry behavior to just about anything. It originates from a fork of retrying which is sadly no longer maintained. Tenacity isn't api compatible with retrying but adds significant new functionality and fixes a number of longstanding bugs.
Questions tagged [python-tenacity]
20 questions
27
votes
7 answers
python retry with tenacity, disable `wait` for unittest
I am using the tenacity library to use its @retry decorator.
I am using this to make a function which makes a HTTP-request "repeat" multiple times in case of failure.
Here is a simple code snippet:
@retry(stop=stop_after_attempt(7),…

DanEEStar
- 6,140
- 6
- 37
- 52
7
votes
2 answers
Python retry package - tenacity : How to log the root cause of exception?
As discussed in this question I am using tenacity to do retries.
A toy code looks like below
import logging
from tenacity import retry
import tenacity
@retry(wait=tenacity.wait_incrementing(start=10, increment=10, max=100),…

tuk
- 5,941
- 14
- 79
- 162
6
votes
3 answers
Python tenacity: How do I retry a function without raising an exception if all retries fail?
let's say I have the following function:
@retry(stop=stop_after_attempt(3))
def foo():
try:
response = requests.post(...)
response.raise_for_status()
return response
except Exception as e:
raise e
This function will retry 3…

JTa
- 181
- 1
- 12
6
votes
1 answer
Python retry using the tenacity module
I'm having having difficulty getting the tenacity library to work as expected. The retry in the following test doesn't trigger at all. I would expect a retry every 5 seconds and for the log file to reflect the retry attempts.
import paramiko
import…

Clive van Hilten
- 851
- 5
- 16
- 32
5
votes
4 answers
Python tenacity: How to retry if exception is NOT of a certain type?
How to retry a function if the exception is NOT of a certain type using Python's tenacity?
retry_if_exception_type will retry if there is risen an exception of a certain type. not does not seems to work placed before the method nor before its…

Nikolay Shindarov
- 1,616
- 2
- 18
- 25
3
votes
1 answer
Tenacity output the messages of retrying?
The code
import logging
from tenacity import retry, wait_incrementing, stop_after_attempt
import tenacity
@retry(wait=wait_incrementing(start=10, increment=10, max=100), stop=stop_after_attempt(3))
def print_msg():
logging.info('Hello')
…

ca9163d9
- 27,283
- 64
- 210
- 413
3
votes
0 answers
Best way to use httpx async client and tenacity?
I'm getting fairly different results with two different implementations.
Here is implementation 1
request_semaphore = asyncio.Semaphore(5)
async def _send_async_request(client: AsyncClient, method, auth, url, body):
async with request_semaphore:
…

JTa
- 181
- 1
- 12
3
votes
1 answer
"Retry" from tenacity module doesn't work with a generator
I have an issue using the "retry" tool from the tenacity library in python3.
The "retry" decorator does not seem to work when I use a generator.
I have a code sample to illustrate my situation:
from tenacity import retry,…

Bibimrlt
- 158
- 1
- 2
- 10
2
votes
2 answers
Python retry using tenacity without decorator
I am trying to do a retry using tenacity (without decorator). My code looks like as explained here.
import logging
from tenacity import retry
import tenacity
def print_msg():
try:
logging.info('Hello')
logging.info("World")
…

tuk
- 5,941
- 14
- 79
- 162
1
vote
0 answers
Connection Error when Sending Multithreaded Requests to an SMS API
I'm developing a Python script to send SMS messages through an API. I'm using multithreading to reduce the sending time. However, I'm encountering an issue where the connection gets aborted occasionally, despite having exception handling in…

Ghost
- 11
- 1
1
vote
0 answers
Retry func if it takes longer than 5 seconds or if it fails
I m in a situation where I have a func that calls the 3rd party API using the native lib of that company. Usually, the response time is excellent, like 1-2 secs but sometimes, the func would take 30-50 seconds to get the response. I want to make the…

PanDe
- 831
- 10
- 21
1
vote
0 answers
Logging python tenacity retry_state with logger from outer scope
I have a module that includes a utility function with a tenacity retry tag
from tenacity import retry, stop_after_attempt, wait_random
def log_attempt_number(logger, retry_state):
logger.info(f"Attempt number {retry_state}")
logger.info("Logger…

OJT
- 887
- 1
- 10
- 26
1
vote
0 answers
python multi threading session with msqyl pooling
import requests, pymysql, pymysqlpool
from concurrent.futures import ThreadPoolExecutor, as_completed
from functools import partial
from tenacity import retry, TryAgain, stop_after_attempt
pool = PooledDB(creator = pymysql,
…

user5151477
- 11
- 2
1
vote
1 answer
Using try-except sentence in python with tenacity doesn't retry as expected
Hi there i'm trying to use tenacity for sending an email, the script is the following:
from tenacity import retry, stop_after_attempt
from smtplib import SMTP_SSL, SMTP
@retry(stop = stop_after_attempt(7))
def email_tables(mails_to_list, smtp_host,…

Seba Rossi
- 91
- 7
1
vote
1 answer
Python Tenacity log exception on retry
I'm using the tenacity package to retry a function. My retry decorator looks like this:
@retry(wait=wait_exponential(multiplier=1/(2**5), max=60), after=after_log(logger, logging.INFO))
On exception I get a logging message like…

David Parks
- 30,789
- 47
- 185
- 328