Skip to content

← Concurrency with Threads and asyncio step 17 of 56

Easy Primitives

Download Pages at the Same Time with asyncio

This is the thread pool exercise again, written with asyncio. You get an async fake web client, and you write the coroutine that downloads every page at the same time.

FakeAsyncWeb.fetch(url) is a coroutine. It pauses briefly, like a slow server, then returns the page. It also records the most fetches that were in progress at the same time.

await download_all(FakeAsyncWeb(), ["a", "b"])
# ["<html>a</html>", "<html>b</html>"]

What to write

Fill in the coroutine download_all(web, urls):

  • Fetch every URL with await web.fetch(url).
  • Start all the fetches before waiting for any of them, so they all run at the same time.
  • Return the pages in the same order as urls. URLs that contain "slow" take longer, so the tests can check the order.

urls can be empty. You do not need asyncio.run, because solve already calls it for you.

What the driver checks

Keep FakeAsyncWeb and solve as they are. solve returns:

  • pages: the list download_all returned.
  • peak: the most fetches in progress at the same time. When every fetch runs at the same time, it equals the number of URLs. Awaiting the fetches one by one in a loop gives 1.