Nesting multiple decorators breaks waiting times
See original GitHub issueLook at this simple example and notice the waiting times does not increase as expected:
In [23]: def backoff_hdlr(details):
...: print ("Backing off {wait:0.1f} seconds afters {tries} tries "
...: "calling function {target} with args {args} and kwargs "
...: "{kwargs}".format(**details))
...:
...: @backoff.on_exception(backoff.expo, ValueError,on_backoff=backoff_hdlr)
...: @backoff.on_exception(backoff.expo, TypeError,on_backoff=backoff_hdlr)
...: def get_url(url):
...: raise ValueError
...:
...: get_url("")
Backing off 0.8 seconds afters 1 tries calling function <function get_url at 0x10a7e2048> with args ('',) and kwargs {}
Backing off 1.3 seconds afters 2 tries calling function <function get_url at 0x10a7e2048> with args ('',) and kwargs {}
Backing off 3.2 seconds afters 3 tries calling function <function get_url at 0x10a7e2048> with args ('',) and kwargs {}
Backing off 0.5 seconds afters 4 tries calling function <function get_url at 0x10a7e2048> with args ('',) and kwargs {}
Backing off 2.6 seconds afters 5 tries calling function <function get_url at 0x10a7e2048> with args ('',) and kwargs {}
Backing off 3.7 seconds afters 6 tries calling function <function get_url at 0x10a7e2048> with args ('',) and kwargs {}
Backing off 2.3 seconds afters 7 tries calling function <function get_url at 0x10a7e2048> with args ('',) and kwargs {}
Backing off 90.7 seconds afters 8 tries calling function <function get_url at 0x10a7e2048> with args ('',) and kwargs {}
Backing off 175.3 seconds afters 9 tries calling function <function get_url at 0x10a7e2048> with args ('',) and kwargs {}
Backing off 146.8 seconds afters 10 tries calling function <function get_url at 0x10a7e2048> with args ('',) and kwargs {}
Backing off 479.5 seconds afters 11 tries calling function <function get_url at 0x10a7e2048> with args ('',) and kwargs {}
(see the 0.5 seconds in the 4th log)
If we just leave one exponential backoff now behaves as expected:
In [25]: def backoff_hdlr(details):
...: print ("Backing off {wait:0.1f} seconds afters {tries} tries "
...: "calling function {target} with args {args} and kwargs "
...: "{kwargs}".format(**details))
...:
...: @backoff.on_exception(backoff.expo, ValueError,on_backoff=backoff_hdlr)
...: def get_url(url):
...: raise ValueError
...:
...: get_url("")
Backing off 0.2 seconds afters 1 tries calling function <function get_url at 0x10a32d620> with args ('',) and kwargs {}
Backing off 0.2 seconds afters 2 tries calling function <function get_url at 0x10a32d620> with args ('',) and kwargs {}
Backing off 2.1 seconds afters 3 tries calling function <function get_url at 0x10a32d620> with args ('',) and kwargs {}
Backing off 4.8 seconds afters 4 tries calling function <function get_url at 0x10a32d620> with args ('',) and kwargs {}
Backing off 10.5 seconds afters 5 tries calling function <function get_url at 0x10a32d620> with args ('',) and kwargs {}
Tested with backoff ==1.8.1
and saw no relevant changes in the changelog since then.
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Python Decorators: From Simple Decorators to Nesting Multiple
To nest decorators, one needs to specify them one at a time before the actual decorated method using the same mechanism used before...
Read more >Python inner functions/decorators: When should I use ...
Let's break this down into two parts. 1) Let's ignore decorators for now. You should use parentheses when you want to call some...
Read more >Nested Decorators in Python - Javatpoint
A function can be decorated multiple times. The nested decorators are also known as the chaining of the decorators. To create the nested...
Read more >Python — Best Practices for Writing Decorators - Dev Genius
Less nesting levels: Use @wrapt.decorator to reduce two levels of nesting to one · Simpler: special cases like class instances can be ignored ......
Read more >Decorators — Uplink 0.9.7 documentation - Read the Docs
Field annotation to specify JSON fields separately, across multiple arguments: Example ... Time to wait for a server response before giving up.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Woah, I just looked at my own at example more carefully and it’s weirder than that. The KeyError wait generator is getting reset each time. I think combining decorators doesn’t work as neatly as was supposed. 😕
Yes, just hitting an API and if I get an exception related with bad formatting I don’t want to retry, but if I hit any other exception I want to back off exponentially.