1.Timeouts
A timeout is the maximum time you let a call to another service run before giving up and raising an error yourself. Without one, a hung downstream service hangs your service too — forever, not just slowly.
Key features
- Every network call needs an explicit timeout — the default in most Python HTTP clients is "no timeout" (wait forever)
- A timeout that fires raises an exception (socket.timeout, httpx.TimeoutException) — it does not return a partial or empty result silently
- Connect timeout (time to establish the connection) and read timeout (time to receive a response) are separate settings — set both
- A timeout that is too short turns healthy-but-slow calls into false failures; too long defeats the point of having one at all
- A timeout bounds ONE attempt — it says nothing about whether to try again, which is what retries decide
Timeout knobs on Python's common HTTP clients
- Call starts — deadline set, e.g. 5s
- No response by deadline — downstream may be alive or dead — unknown
- Timeout fires — raises an exception NOW, not eventually
Command
import httpx
with httpx.Client(timeout=5.0) as client:
try:
response = client.get(url)
except httpx.TimeoutException:
# decide here: retry, fall back, or surface an error
...Every network call needs an explicit timeout — no timeout means "wait forever," and a hung downstream then hangs you too.