Week 2 · Production Python · about 3 minutes
What you will be able to do
Set connect and read timeouts so a slow dependency becomes a visible error instead of a hang.
The idea
requests and Python's urllib wait forever by default. httpx gives up after 5 seconds, which may not suit your call either. A slow dependency then holds your worker, your thread and your customer's request, silently.
Set two numbers on every call. Connect timeout: how long to reach the server, usually a few seconds. Read timeout: how long to wait for the answer, sized to what the caller can tolerate.
A timeout turns a hang into an error you can see, log and retry.
import httpx
client = httpx.Client(
timeout=httpx.Timeout(connect=3.0, read=10.0, write=10.0, pool=3.0)
)
def get_prices(book: str) -> dict:
response = client.get(f"{PRICING_URL}/books/{book}")
response.raise_for_status()
return response.json()No timeout is a decision too: you chose to wait forever.
Key takeaways
- Set both a connect and a read timeout.
- Size the read timeout to what the caller can wait.
- A timeout is an error you can log and retry; a hang is not.
Part of the Forward Deployed Engineer roadmap. New to the role? Start with what a Forward Deployed Engineer does.