Requests-futures is a small add-on for the popular 'requests' HTTP library, enabling asynchronous HTTP requests. It leverages Python's `concurrent.futures` module to perform requests concurrently using either `ThreadPoolExecutor` or `ProcessPoolExecutor`. It provides a `FuturesSession` class that mimics `requests.Session` but returns `Future` objects, allowing non-blocking operations and retrieval of responses later. The current version is 1.0.2, and its release cadence is infrequent but active.
pip install requests-futuresVerified import paths — ran on the pinned version, not inferred.
Initialize a FuturesSession, submit requests which return Future objects, then iterate through the Futures and call `.result()` to obtain the Response objects once they are ready. Error handling for network issues should wrap the `.result()` call.
Ensure all components (session, request data, hooks) are picklable. For Python 3.4 using `ProcessPoolExecutor`, instantiate `FuturesSession(executor=ProcessPoolExecutor(), session=requests.Session())`. Python 3.5+ handles this more gracefully.
Wrap calls to `future.result()` in a `try/except` block to catch network errors or other request-related exceptions.
Always explicitly set `max_workers` when initializing `FuturesSession` with `ThreadPoolExecutor` to ensure predictable concurrency levels, e.g., `FuturesSession(executor=ThreadPoolExecutor(max_workers=10))`.
Always specify a `timeout` when making requests, e.g., `session.get(url, timeout=5)`. This timeout applies to `future.result()` as well.
pip install requests-futures
future = session.get(url) response = future.result() print(response.status_code)
from requests_futures.sessions import FuturesSession