Captain
Captain

Reputation: 143

How to retry python requests.get without using sessions

I want to retry the python requests.get(). I know we can do it with sessions but in a specific case I need to retry the requests.get() only so I wanted to know how do that? Is there any parameter like in session that we can pass?

Upvotes: 2

Views: 5721

Answers (1)

Alex Shani
Alex Shani

Reputation: 161

you can use backoff library, I'm using it and I think that it's a cleaner solution than adapters and sessions

A basic example:

import backoff

@backoff.on_exception(
    backoff.expo,
    requests.exceptions.RequestException,
    max_tries=5,
    giveup=lambda e: e.response is not None and e.response.status_code < 500
)
def publish(self, data):
    r = requests.post(url, timeout=10, json=data)
    r.raise_for_status()

Upvotes: 2

Related Questions