Skip to content

Instantly share code, notes, and snippets.

@dtheodor
Last active October 4, 2020 21:06
Show Gist options
  • Save dtheodor/4996d463aba0b1c23164a69aca540ead to your computer and use it in GitHub Desktop.
Save dtheodor/4996d463aba0b1c23164a69aca540ead to your computer and use it in GitHub Desktop.
requests.Session, retry on network errors
import requests
from requests.adapters import HTTPAdapter
from urllib3.util.retry import Retry
def create_session(num_hosts: int, num_workers: int, retry: int) -> requests.Session:
session = requests.Session()
adapter = HTTPAdapter(
pool_connections=num_hosts, # number of different hosts we will be talking to
pool_maxsize=num_workers, # number of maximum concurrent requests the session will be doing (i.e. with multiple threads that share it)
max_retries=Retry(
total=retry,
read=retry,
connect=retry,
status=retry,
backoff_factor=0.3,
status_forcelist=Retry.RETRY_AFTER_STATUS_CODES.union((500, 502, 503, 504)),
),
)
session.mount("http://", adapter)
session.mount("https://", adapter)
return session
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment