python-3.xurllib3

urllib3 - Timeout and Retry


We would like to set the timeout for our REST call. However, when the timeout is reached, will it automatically do a retry (up to the default max of 3)? The documentation is not real clear.

http = urllib3.PoolManager(cert_reqs='CERT_REQUIRED', ca_certs=os.getenv('SSL_CERT_FILE'))

try:

    resp = http.request(
        'GET',
        newurl,
        decode_content=True,
        headers={'Accept': 'application/json'},
        timeout=1
    )

Solution

  • Yeah, we ended up using this approach.

    retry = Retry(
        total=3, 
        raise_on_status=True, 
        backoff_factor=0.1, 
        status_forcelist=[429,500,502,503,504]
    )
    timeout = Timeout(1.0)
    
    http = urllib3.PoolManager(
        cert_reqs='CERT_REQUIRED', 
        ca_certs=os.getenv('SSL_CERT_FILE'), 
        retries=retry, 
        timeout=timeout
    )
    
    try:
        resp = http.request(
            'GET',
            newurl,
            decode_content=True,
            headers={'Accept':'application/json'}
        )