APIs and Async · Building an API Client · lesson 12 of 12
Rate limits and backoff
about 16 minutes · free · runs in your browser
When the server asks you to slow down
429 Too Many Requests means you are going too fast. Politeness here is practical: an API that keeps rate-limiting you will eventually block you.
A 429 usually carries a Retry-After header saying how many seconds to wait. Honour it
when it is there, and fall back to exponential backoff when it is not:
if r.status_code == 429:
wait = float(r.headers.get("retry-after", 2 ** attempt))
time.sleep(wait)
continue
The fake /limited endpoint returns 429 once, with retry-after: 0, then succeeds.
Your turn: write fetch_polite(url, max_attempts=4) that honours Retry-After on
a 429, returns the parsed body on success, and None if it never succeeds. Record the
waits it performed in a module-level waits list rather than actually sleeping, so the
behaviour can be checked.
You start from this, and edit it in the browser:
import fake_requests as requests
waits = []
def fetch_polite(url, max_attempts=4):
# On a 429, append the wait to `waits` and try again.
pass