requests-futures
Asynchronous Python HTTP requests, for humans, using requests and Python’s built-in concurrent.futures.
requests_futures.sessions.FuturesSession is a drop-in-ish
Session subclass: every request-making method (get,
post, head, …) still works exactly as it does on a plain
Session, except it returns a Future
immediately, instead of blocking for a Response. The
request itself runs in a background thread (or process, if you supply a
ProcessPoolExecutor), so several requests can be
in flight at once.
Install
pip install requests-futures
Quickstart
from requests_futures.sessions import FuturesSession
session = FuturesSession()
# the request is sent immediately, in the background
future = session.get('https://httpbin.org/get')
# ... do other work here while it's in flight ...
# blocks only if the response isn't back yet
response = future.result()
print(response.status_code, response.json())
See Usage for the fuller picture: multiple requests with
as_completed, error handling across the future boundary, retries,
sizing the worker pool, streaming, sharing an executor across sessions,
hooks, ProcessPoolExecutor, and the thread-safety caveats that come
with sharing a Session.
Documentation
- Usage
- Basic usage
- Waiting with
as_completedand a timeout - Error handling across the future boundary
- Retries via a mounted
HTTPAdapter - Sizing
max_workersagainst the connection pool - Streaming
- Sharing one executor across several sessions
hooks(the recommended replacement forbackground_callback)- Using
ProcessPoolExecutor - Thread-safety caveat
- API Reference