Search the courses, subjects, tracks, games and features, or jump straight to a page.
5 cards from this section. Quick, good the night before a test.
What does await asyncio.sleep(1) do inside a simulated network call, and why is it used instead of time.sleep(1)?
Ask what the event loop can do while each kind of sleep is happening.
Write an asynchronous function simulate_download that takes a file name and simulates downloading it by waiting a random time between 2 and 5 seconds. Then print a message indicating the file has been downloaded.
Use asyncio.sleep to simulate delay and random.uniform for random times.
What does await asyncio.sleep(2) do inside a coroutine?
Compare it with `time.sleep()`.
Three simulated calls that take 1, 2 and 3 seconds run together with asyncio.gather(). About how long does the whole thing take?
The waits overlap rather than add up.
Turn the download exercise into a small download manager. The exercise asked:
Question: Write an asynchronous function simulate_download that takes a file name and simulates downloading it by waiting a random time between 2 and 5 seconds. Then print a message indicating the file has been downloaded.
Now make simulate_download return its delay, download three files concurrently, and print the sum of the delays, the slowest delay and the real total time. Show that the total is close to the slowest download, not the sum.
`asyncio.gather()` returns each coroutine's return value, so returning the delay gives you the numbers to compare.
A simulated API fails half the time. Write fetch_with_retry(site, attempts=3) that retries a failed call with a growing pause between attempts and gives up after the last attempt. Fix the random seed so the run is repeatable, and print every attempt.
Use a `for` loop over the attempt numbers, and re-raise the exception with a bare `raise` on the final attempt.
6 more questions in this section. Enroll free to practise them all.
Enroll free