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

Default timeout with retries #1393

Closed
foxx opened this issue May 29, 2013 · 3 comments
Closed

Default timeout with retries #1393

foxx opened this issue May 29, 2013 · 3 comments

Comments

@foxx
Copy link

foxx commented May 29, 2013

At the moment if I specify a timeout on a request, it does not attempt to retry.

It would be nice to be able to specify a default timeout that adhered to the default retry.

Here is the workaround I used for now;


requests.adapters.DEFAULT_RETRIES = 3

def fetch_page(url, timeout=3):
    c = 0
    last_exc = None
    while True:
        if c >= requests.adapters.DEFAULT_RETRIES:
            raise last_exc[0], last_exc[1], last_exc[2]
        try:
            return requests.get(url, timeout=timeout)
        except requests.Timeout, e:
            c+=1
            last_exc = sys.exc_info()
            logging.info("request timeout, retrying.. (%s/%s)" % ( c, 
                requests.adapters.DEFAULT_RETRIES))
        except Exception, e:
            raise

@Lukasa
Copy link
Member

Lukasa commented May 29, 2013

The intended API for retries is to use a different transport adapter:

import requests
from requests.adapters import HTTPAdapter

s = requests.Session()
s.mount('http://', HTTPAdapter(max_retries=3))
s.mount('https://', HTTPAdapter(max_retries=3))

You can then set the timeout on each request and this should work.

Does this look like what you want?

@foxx
Copy link
Author

foxx commented May 29, 2013

Ah that makes sense.. I couldn't figure this out from the docs, so perhaps the docs could be improved.. Although I'm not too sure how it would be explained

Thanks for the quick reply!

@Lukasa
Copy link
Member

Lukasa commented May 29, 2013

I'll have a think about how best to document this, but I might just end up writing a blog post about it and calling it a day.

Thanks for opening the issue, let me know if you need anything else! =)

@Lukasa Lukasa closed this as completed May 29, 2013
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Sep 9, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants