Python retry using the tenacity module

2024/10/14 16:27:12

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 tenacity
from configparser import RawConfigParser
import loggingdef main():parser = RawConfigParser()parser.read('config.ini')HOST = parser['SSH']['host']PORT = parser['SSH']['port']USER = parser['SSH']['user']LOG_LEVEL = parser['Logging']['level']LOG_FILE = parser['Files']['log_file']LOG_FORMAT = parser['Logging']['format']with open(LOG_FILE, "a") as f:logging.basicConfig(filename=LOG_FILE,level=LOG_LEVEL,format=LOG_FORMAT)logging.info("******Started logging******")client = paramiko.SSHClient()client.set_missing_host_key_policy(paramiko.AutoAddPolicy())@tenacity.retry(wait=tenacity.wait_fixed(5))def connect():try:client.connect(HOST, int(PORT), USER)logging.info("Successful connection to remote server")except Exception:logging.error("Cannot connect to remote server")connect()if __name__ == "__main__":main()

The log file spits this out:

> 2017-11-04 19:55:20,695 ******Started logging******
> 2017-11-04 19:55:20,695 Cannot connect to remote server
Answer

I suppose that tenacity detects failures by handling exceptions. In your code the exception is logged and then discarded. So, for tenacity, your call is actually successful. You should re-raise this exception to get the expected behavior. Like this:

@tenacity.retry(wait=tenacity.wait_fixed(5))
def connect():try:client.connect(HOST, int(PORT), USER)logging.info("Successful connection to remote server")except Exception:logging.error("Cannot connect to remote server")raise
https://en.xdnf.cn/q/69393.html

Related Q&A

How to write own logging methods for own logging levels

Hi I would like to extend my logger (taken by logging.getLogger("rrcheck")) with my own methods like: def warnpfx(...):How to do it best? My original wish is to have a root logger writing …

How to use pandas tz_convert to convert to multiple different time zones

I have some data as shown below with hour in UTC. I want to create a new column named local_hour based on time_zone. How can I do that? It seems like pandas tz_convert does not allow a column or panda…

virtualenv, python and subversion

Im trying to use the python subversion SWIG libraries in a virtualenv --no-site-packages environment. How can I make this work?

Float to Fraction conversion in Python

While doing exercise on the topic of float type to Fraction type conversion in Python 3.52, I found the difference between the two different ways of conversion.The first method is:>>> from fra…

How to update an SVM model with new data

I have two data set with different size.1) Data set 1 is with high dimensions 4500 samples (sketches).2) Data set 2 is with low dimension 1000 samples (real data). I suppose that "both data set ha…

Expanding NumPy array over extra dimension

What is the easiest way to expand a given NumPy array over an extra dimension?For example, suppose I have>>> np.arange(4) array([0, 1, 2, 3]) >>> _.shape (4,) >>> expand(np.…

Django-Haystack giving attribute error?

I am trying to use Haystack and Whoosh with my Django app. I followed the steps on Haystack docs, but i am getting this error when i do a searchAttributeError at /search/ module object has no attribute…

python calendar with holidays [duplicate]

This question already has answers here:Closed 12 years ago.Possible Duplicate:Holiday Calendars, File Formats, et al. Hi, Is there a calendar library in Python with which I can check for holidays, com…

How to choose your conda environment in Jupyter Notebook

I installed Anaconda 5.3 with Python 3.7 (root environment). After that I created a new environment (py36) using Python 3.6I activated the new environment with activate py36 conda env list shows that t…

How do I stagger or offset x-axis labels in Matplotlib?

I was wondering if there is an easy way to offset x-axis labels in a way similar to the attached image.