What this thread does
Do first: reuse one httpx.AsyncClient around a bounded batch loop and configure phase-specific Timeout values before increasing concurrency.
Short answer
As of November 2026, the baseline is HTTPX 0.28.1, with the existing Python 3.11 runtime baseline. The settings below are an initial application policy, not an upstream quota. Pool connection count is not a request-per-second limiter;
Choose / don't choose
- Finite small input: start batches of at most ten operations; avoid creating one task per record for unbounded input. A slow request delays the next batch.
- Large/continuous input: use a fixed worker count and bounded input queue; keep only incremental results. A semaphore with millions of pending tasks still retains those tasks.
- Retry: transport connection retries cover ConnectError/ConnectTimeout; application backoff handles classified status/read failures. Retry mutations only with a safe outcome/idempotency contract (transport retry scope).
Not for: FastAPI handler blocking; see python-coroutine-never-awaited-and-fastapi-async-def-blocking.
Settings that matter
| Setting or flag | Value / default | Applies to | Source |
|---|---|---|---|
httpx.Timeout(..., connect=...) | Example policy 5.0 seconds for connection establishment; tune to observed connection latency | HTTPX baseline above | timeouts |
| Read and write timeouts | Example policy 10.0 / 10.0 as the maximum duration to wait for data chunks; not whole-request deadlines | HTTPX baseline above | timeouts |
| Pool timeout | Example policy 5.0 for waiting to acquire a connection; default timeout behavior is 5 seconds | HTTPX baseline above | timeouts |
Limits.max_connections | Example 10; default 100; pair with at most ten active operations | HTTPX baseline above | limits |
Limits.max_keepalive_connections | Example 5; default 20; number of allowable keep-alive connections | HTTPX baseline above | limits |
AsyncHTTPTransport(retries=...) | Example 1 connection retry; default 0. Pass limits=limits to this transport if explicitly constructed | HTTPX baseline above | transport guide; implementation |
Order of operations
1. Create limits and timeout once; if supplying an explicit AsyncHTTPTransport, give its connection pool the limits. Open one AsyncClient before creating request tasks (async ownership). 2. Inside that client scope, create a TaskGroup for each bounded batch. Its first ordinary task failure cancels siblings and waits for them before leaving the scope. Handle per-record errors within workers when independent records should continue. Let cancellation propagate after cleanup (TaskGroup). 3. Close each streamed response with its async context; manual stream mode requires ensuring Response.aclose is called eventually; use a finally path for explicit ownership. Drain/cancel and join request tasks before leaving the client scope; do not close the shared client while siblings still use it (streaming). 4. Record active operation count, application admission wait, stream lifetime, request elapsed time and exception phase. Compare connect latency and time-to-first/next-byte. If pool failures persist at low load, inspect leaked streams and transport health; kdavh’s January 30, 2023 discussion and Heiaha’s December 17, 2025 follow-up document persistent pool failure without establishing a universal cause. 5. Use an application total deadline around all attempts and backoff (for this Python baseline, asyncio.timeout). Cap attempts and delay, add jitter, and honor the upstream rate-limit/retry contract. An HTTPStatusError may be raised by raise_for_status; connection retries cover ConnectError/ConnectTimeout. In a controlled endpoint fixture record peak in-flight operations and inject each timeout/cancellation path (deadline API).
Known failures and fixes
| Error (short quote) | Cause | Fix | Source |
|---|---|---|---|
PoolTimeout | No pool connection became available within the pool wait; high concurrency or unreleased responses are possible | Inspect active count and stream lifetimes; close responses and reduce admission before raising the pool size | timeout definitions |
ConnectTimeout | Connection establishment exceeded its phase budget | Inspect connection/DNS/TLS/network latency; bounded connection retries may help transient failures | exceptions |
ReadTimeout | No response chunk arrived within its phase budget; remote effect may already have happened | Inspect upstream processing and inter-chunk delays; use application retry/backoff only when replay is safe | exceptions; transport scope |
Missing
- Upstream quota, acceptable overall deadline and replay semantics are unknown. HTTPX timeout phases bound inactivity/waiting, not total operation duration. The persistent-pool discussion does not prove that batching repairs all transport faults.
Checked
2026-11-12; HTTPX; 0.28.1 release; async/timeouts/limits/transports/exception references, tagged transport implementation, Python tasks docs and kdavh/Heiaha reports read. No application commands were executed.
Started from a post by @Heiaha, packaged by the editor. Original post