question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Backoff code inside function

See original GitHub issue

I am calling some functions of a lib i wanna backoff. Problem is, I cannot modify the lib.

import backoff
from requests.exceptions import TimeoutError
from somewhere import library

So I am looking for something like:

with backoff.on_exception(backoff.expo, TimeoutError, max_tries=8):
    library.some_function(yada, blah, foo, bar)
# end if

or

while backoff.on_exception(backoff.expo, TimeoutError, max_tries=8):
    library.some_function(yada, blah, foo, bar)
# end if

Not sure what is possible to do.

Edit (2017-04-27): Calling just one function can be solved like seen below

Issue Analytics

  • State:open
  • Created 7 years ago
  • Reactions:1
  • Comments:10 (5 by maintainers)

github_iconTop GitHub Comments

6reactions
luckydonaldcommented, Apr 27, 2017

I just realized: If it is a just a single function - even in a library - you could supply the decorator yourself: So as solution to just the problem written in the original posting (calling a single function) this should be sufficient:

from somewhere.library import some_function as _some_function
import backoff
from requests.exceptions import TimeoutError

some_function = backoff.on_exception(backoff.expo, TimeoutError, max_tries=8)(_some_function)

some_function(yada, blah, foo, bar)
1reaction
luckydonaldcommented, Apr 27, 2017

@pquentin, to which of my suggestion did you refer?

@bgreen-litl, What about the for+with? It also looks handy for normal code you don’t really wan’t to put in an extra function.

So 2 lines would be still less than writing it yourself every time:

for retry in backoff.retry_on_exception(backoff.expo, TimeoutError, max_tries=5):
   with retry:
     # >>> do stuff here <<<
   # end with
# end for

Now manually:

timeout = backoff.expo()
for try_count in range(5):
  try:
     # >>> do stuff here <<<
     break  # because executed successfully
  expect TimeoutError as e:
    if try_count == 5:
      raise e
    # end if
   sleep(timeout.next())
  # end try
# end for  
Read more comments on GitHub >

github_iconTop Results From Across the Web

litl/backoff: Python library providing function decorators for ...
Backoff supports asynchronous execution in Python 3.5 and above. To use backoff in asynchronous code based on asyncio you simply need to apply...
Read more >
Using the Library — Backoff-Utils 1.0.1 documentation
This library provides a simple one-line approach to using backoff strategies in your code. It provides a simple function ( backoff() ) and...
Read more >
Exponential backoff algorithm in NodeJS | by Rajkumar Gaur
The code for the backoff function is wrapped inside a setTimeout that waits for a time of Math.pow(2, exponent) + Math.random() * 1000...
Read more >
How to implement an exponential backoff retry strategy in ...
And second, make sure to put a limit to the number of retries so that the code eventually gives up and throws an...
Read more >
Implementing exponential backoff | Cloud IoT Core ...
random_number_milliseconds is a random number of milliseconds less than or equal to 1000. This helps to avoid cases in which many clients are...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found