Code Quiz

Blocking call inside asyncio coroutine

Spot why this asyncio download routine runs serially instead of concurrently.

Codepython
import asyncio
import time
import requests

async def fetch(url):
    print(f"start {url}")
    # simulate an I/O-bound HTTP request
    resp = requests.get(url)
    print(f"done {url}: {len(resp.content)} bytes")
    return resp.status_code

async def main():
    urls = ["https://example.com"] * 5
    start = time.perf_counter()
    results = await asyncio.gather(*(fetch(u) for u in urls))
    print(f"elapsed {time.perf_counter() - start:.2f}s")
    return results

asyncio.run(main())

Why does this code fail to run the five fetches concurrently despite using asyncio.gather?