Comments (5)
With the except
block, code is not retrying at all because when exception is being raise first time, it is being handled by except
block. So, backoff library is not considering the fact that function is throwing any exception in the first place.
from backoff.
The documentation states:
When a give up event occurs, the exception in question is reraised and so code calling an on_exception-decorated function may still need to do exception handling.
So you should be able to do:
@backoff.on_exception(backoff.expo,
requests.exceptions.RequestException,
on_backoff=backoff_hdlr)
def get_url(url):
return requests.get(url)
def get_url_or_default(url, default=None):
try:
return get_url(url)
except requests.exceptions.RequestException:
return default
Note: Code is an untested draft.
from backoff.
@PatDue is correct. With on_exception
, once the retrying is done, the exception that caused retry is raised and you can handle that exception and do whatever you like.
@sagarr I should note though, that in your code snippet you don't specify any of the keyword args that would cause backoff to give up at all. You need to use max_tries
or max_time
or giveup
to trigger backoff.on_exception to stop trying.
from backoff.
@mayank-kgp I don't know what you mean. Can you provide example code if you're reporting a bug?
from backoff.
Hi,
I might be missing something here, I tried looking into docs and all issues, but didn't get anything..
Here what I want, I want to return default value once all the retrying is done, like
def backoff_hdlr(details): return None # tried getting value from URL, now just return None @backoff.on_exception(backoff.expo, requests.exceptions.RequestException, on_backoff=backoff_hdlr) def get_url(url): return requests.get(url)
Hi, I am facing the same problem as well. Were you able to solve this issue?
Is there a way for backoff decorator to return the value of backoff_hdlr as value of get_url function?
from backoff.
Related Issues (20)
- 2.2.0 release gives type errors for previously valid usage? HOT 1
- Max tries
- Duplicate logging in some special cases HOT 1
- How do I use this?
- Obtain value returned by backoff_handler function after maximum tries is reached
- max_retries HOT 1
- max_time only applies _after_ first retry?
- Canonical way to handle error bursting
- Add exponential decay
- How to handle backoff on Facebook cursor? HOT 1
- Improve Typing and Error Handling in `on_exception`
- question about max_time
- Feature Request: add a way to use backoff in a context manager HOT 2
- feature request: toggle decorator on/off
- `backoff.on_predicate` does not work for `staticmethod`s
- Expose typing hints as they are part of the API
- Add support for globally disabling retries
- How to provide arguments to `on_backoff` function HOT 1
- Feature Request: support direct invocation with functools.partial
- Maintenance help? HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from backoff.