Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide backoff context managers #40

Closed
OddBloke opened this issue Jul 6, 2017 · 2 comments
Closed

Provide backoff context managers #40

OddBloke opened this issue Jul 6, 2017 · 2 comments

Comments

@OddBloke
Copy link

OddBloke commented Jul 6, 2017

I sometimes have a third-party function or method that I would like to retry using backoff. Currently, I have to wrap the call in a single line function and decorate that. It would be nice if I could, instead, use a context manager.

So what I currently do is:

def do_a_thing(...):
    @backoff.on_exception(...)
    def _actually_do_third_party_thing():
        _do_third_party_thing()

    some_setup()
    _actually_do_third_party_thing()
    some_teardown()

And what I'd like to be able to do:

def do_a_thing(...):
    some_setup()
    with backoff.on_exception(...):
        _do_third_party_thing()
    some_teardown()
@OddBloke
Copy link
Author

OddBloke commented Jul 6, 2017

I've spent a bit of time digging in to this, and I think it's actually impossible to do with the context manager semantics as they currently exist.

Specifically, there's no way to re-execute the body of a with statement, which is pretty important in this context[0].

[0] https://mail.python.org/pipermail/python-ideas/2013-May/020633.html

@OddBloke OddBloke closed this as completed Jul 6, 2017
@bgreen-litl
Copy link
Member

bgreen-litl commented Jul 7, 2017

This has come up before and there is some ongoing discussion here #18

And you are correct that this can't be done as originally suggested. We've talked about a few variants of this idea but I've never quite been able to make it work in a way that is a clear improvement to the status quo. FWIW, in the case where you are calling that 3rd party function in a single place, I recommend the decorating the function inline (as you do in your example above) as a best practice.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants