Python Never Gives Up: The tenacity Library
Python Never Gives Up: The tenacity Library
We take a look at a fork of Pythons' retrying library, aptly called 'tenacity,' why it was created, and what it can do for your exception handling techniques in Python.
Join the DZone community and get the full member experience.Join For Free
Jumpstart your Angular applications with Indigo.Design, a unified platform for visual design, UX prototyping, code generation, and app development.
A couple of years ago, I wrote about the Python retrying library. This library was designed to retry the execution of a task when a failure occurred.
I've started to spread usage of this library in various projects, such as Gnocchi, these last few years. Unfortunately, it started to get very hard to contribute and send patches to the upstream retrying project. I spent several months trying to work with the original author. But after a while, I had to come to the conclusion that I would be unable to fix bugs and enhance it at the pace I would like to. Therefore, I had to make a difficult decision and decided to fork the library.
Here Comes tenacity
I picked a new name and rewrote parts of the API of retrying that were not working correctly or were too complicated. I also fixed bugs with the help of Joshua and named this new library tenacity. It works in the same manner as retrying does, except that it is written in a more functional way and offers some nifty new features.
The basic usage is to use it as a decorator:
import tenacity @tenacity.retry def do_something_and_retry_on_any_exception(): pass
This will make the function
do_something_and_retry_on_any_exception be called over and over again until it stops raising an exception. It would have been hard to design anything simpler. Obviously, this is a pretty rare case, as one usually wants to wait for some time between retries. For that, tenacity offers a large panel of waiting methods:
import tenacity @tenacity.retry(wait=tenacity.wait_fixed(1)) def do_something_and_retry(): do_something()
Or a simple exponential back-off method can be used instead:
import tenacity @tenacity.retry(wait=tenacity.wait_exponential()) def do_something_and_retry(): do_something()
What is especially interesting with tenacity, is that you can easily combine several methods. For example, you can combine
tenacity.wait.wait_fixed to wait a number of seconds defined in an interval:
import tenacity @tenacity.retry(wait=tenacity.wait_fixed(10) + wait.wait_random(0, 3)) def do_something_and_retry(): do_something()
This will make the function being retried wait randomly between 10 and 13 seconds before trying again.
tenacity offers more customization, such as retrying on some exceptions only. You can retry every second to execute the function only if the exception raised by
do_something is an instance of
IOError, e.g. a network communication error.
import tenacity @tenacity.retry(wait=tenacity.wait_fixed(1), retry=tenacity.retry_if_exception_type(IOError)) def do_something_and_retry(): do_something()
You can combine several conditions easily by using the
& binary operators. They are used to make the code retry if an
IOError exception is raised, or if no result is returned. Also, a stop condition is added to the
stop keyword arguments. It allows you to specify a condition unrelated to the function result of exception to stop, such as a number of attempts or a delay.
import tenacity @tenacity.retry(wait=tenacity.wait_fixed(1), stop=tenacity.stop_after_delay(60), retry=(tenacity.retry_if_exception_type(IOError) | tenacity.retry_if_result(lambda result: result == None)) def do_something_and_retry(): do_something()
The functional approach of tenacity makes it easy and clean to combine a lot of conditions for various use cases with simple binary operators.
tenacity can also be used without decorators by using the object
Retrying, that implements its main behavior, and using its
call method. This allows you to call any function with different retry conditions, or to retry any piece of code that does not use the decorator at all – like code from an external library.
import tenacity r = tenacity.Retrying( wait=tenacity.wait_fixed(1), retry=tenacity.retry_if_exception_type(IOError)) r.call(do_something)
This also allows you to reuse that object without creating a one new each time, saving some memory!
I hope you'll like it and will find it of some use. Feel free to fork it, report bug, or ask for new features on its GitHub!
Published at DZone with permission of Julien Danjou . See the original article here.
Opinions expressed by DZone contributors are their own.