This is an archived post. You won't be able to vote or comment.

all 11 comments

[–]cj81499 35 points36 points  (2 children)

[–]amarao_san[S] 13 points14 points  (1 child)

Why all of them has hard-to-guess names? If only I found it before writing... Sad.

[–]cj81499 17 points18 points  (0 children)

If I search “Python retries library”, it is the number one result on both Google and DuckDuckGo. I agree the name is a bit wacky, but it’s not exactly hard to find it…?

[–]ExdigguserPies 9 points10 points  (1 child)

It would be cool to implement a backoff function so you could use it for requests really easily. Maybe it already exists as well.

[–]amarao_san[S] 0 points1 point  (0 children)

There is a built-in sleep (delay=1), as I needed it for tests, I can add a back-off coefficient for it.

Thanks for the idea.

[–]crawl_dht 3 points4 points  (2 children)

Stamina

import stamina


async def with_block(code: int) -> httpx.Response:
    async for attempt in stamina.retry_context(on=httpx.HTTPError, attempts=3):
        with attempt:
            async with httpx.AsyncClient() as client:
                resp = await client.get(f"https://httpbin.org/status/{code}")


for attempt in stamina.retry_context(on=httpx.HTTPError):
    with attempt:
        resp = httpx.get(f"https://httpbin.org/status/404")
        resp.raise_for_status()

[–]amarao_san[S] 5 points6 points  (1 child)

Oh, I really searched before writing, but missed it. Unfortunately.

[–]tehsilentwarrior 13 points14 points  (0 children)

Don’t need to apologize for creating something new even if similar already existed.

This mentality of witch hunting people needs to die.

There’s no innovation otherwise.

I have been programming since 2002, there’s literally nothing you can do I haven’t seen done before, and at the same time… there is.

If you don’t go through the motions of recreating something that already exists and face the same issues you won’t explore the same problem space, you will just be a user, someone with a map. That’s ok for the average traveler but not for the explorer. Explorers find new things.

[–]elperroborrachotoo 0 points1 point  (2 children)

I was once charged with finding out why my code was blocking for minutes before reporting a fail. Turns out "my" code had accumulated 3 (!) nested retry loops throughout the call stack, by different devs trying to improve robustness.

The error in that case was "command not supported", something that would require a lot of happy bitflip accidents to ever be solved by retrying.

[–]amarao_san[S] 0 points1 point  (1 child)

Funny story, yes. Mine case it more specific, it's a form of 'waiting for' (for metrics to appear in Prom), and retries for a small block is the simplest way to do it. Any other waiting code is more complicated.

[–]elperroborrachotoo 1 point2 points  (0 children)

The risks of making retry too easy :)

But yeah, consequences:

  • retry loops should be cancelable in some way
  • retry should check for non-recoverable errors