We can't find the internet
Attempting to reconnect
Something went wrong!
Attempting to reconnect
← Concurrency with Threads and asyncio step 17 of 56
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 listdownload_allreturned.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 gives1.
Your code, saved here
Sign in to access your accepted solutions and saved draft history. You can keep reading the lesson without an account.
Sign in to your accountStuck?
Python reference solution
Sign in to track your attempts toward unlocking the reference solution.