Reuse a Session for Many Calls

Medium ⏱ 12 min 63% acceptance ★★★★☆ 4.4
Opening a new connection per request is slow. Write fetch_many(base_url, ids) creating one requests.Session (in a with block), setting a shared header {'X-Client': 'datavix'} on it, and GETting /items/<id> for each id — returning the list of parsed bodies. The session pools connections across calls.

Examples

Example 1
Input
fetch_many(url, [1, 2, 3])
Output
three parsed bodies over one pooled connection
Explanation

Session headers apply to every request it makes.

Constraints

  • One Session for all calls.
  • Set the header on the session, not per request.

Topics

APIsrequests.Session

Companies

MetaLinkedInSalesforce

Hints

Hint 1

with requests.Session() as s:.

Hint 2

s.headers.update({...}) once.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.