Overview
HTTPX 0.20, released on August 28, 2021, improves HTTP/2 support with request multiplexing and refines the async API.
Main Features
HTTP/2 multiplexing
HTTP/2 multiplexing sends multiple requests simultaneously over a single TCP connection, reducing latency and improving throughput for multiple API calls.
python
import httpx
import asyncio
async def parallel_requests():
async with httpx.AsyncClient(http2=True) as client:
# Multiplexing: simultaneous requests on one connection
tasks = [
client.get('https://httpbin.org/get'),
client.get('https://httpbin.org/headers'),
client.get('https://httpbin.org/ip'),
]
responses = await asyncio.gather(*tasks)
for r in responses:
print(f'{r.url} -> {r.status_code} [{r.http_version}]')
# asyncio.run(parallel_requests())
Improved async API
The async API has been refined with better connection lifecycle management, granular timeout support, and simplified ASGI framework integration.
python
import httpx
# Synchronous client with granular timeouts
timeout = httpx.Timeout(
connect=5.0,
read=10.0,
write=5.0,
pool=2.0,
)
with httpx.Client(timeout=timeout) as client:
r = client.get('https://httpbin.org/get')
print(f'Status: {r.status_code}')
print(f'HTTP version: {r.http_version}')
