The concurrent threads solution from me with configurable pool size: How do connections recycle in a multiprocess pool serving requests from a single requests.Session object in python?
Thanks for this, although I'm not entirely sure how to adapt it for my case?
from concurrent.futures.thread import ThreadPoolExecutor
from functools import partial
from requests import Session, Response
from requests.adapters import HTTPAdapter
list_of_urls = [("www.site.com/" + "{0:04}".format(num)) for num in range(9999)] # one row difference with the solution from link above
def thread_pool_execute(iterables, method, pool_size=30) -> list:
"""Multiprocess requests, returns list of responses."""
session = Session()
session.mount('https://', HTTPAdapter(pool_maxsize=pool_size))
session.mount('http://', HTTPAdapter(pool_maxsize=pool_size))
worker = partial(method, session)
with ThreadPoolExecutor(pool_size) as pool:
results = pool.map(worker, iterables)
session.close()
return list(results)
def simple_request(session, url) -> Response:
return session.get(url)
response_list = thread_pool_execute(list_of_urls, simple_request)