Gather Concurrent Results
Advanced · Write the code · from Python Async/Await: Write Non-Blocking Code from Scratch
From the lesson: Python Async/Await: Write Non-Blocking Code from Scratch
Imagine you're a chef in a busy kitchen. You don't stand and watch the oven for 20 minutes while your pasta boils on another burner. Instead, you start the oven, move to the pasta, then check the salad -- all without waiting idle. That's exactly what asynchronous programming does for your code.
Read the lessonWrite an async def function called double that takes x, waits 0.1 seconds with asyncio.sleep, and returns x * 2. Then run double(1), double(2) and double(3) at the same time with asyncio.gather() and print the list of results.
What Check runs
Your code runs first, then each of these lines. The output must match exactly.
| Runs | Expected output |
|---|---|
| import inspect print(inspect.iscoroutinefunction(double)) | [2, 4, 6] True |
More Async and Await exercises
- Define a CoroutineAdvanced
- Predict the Coroutine OutputAdvanced
- Build an Async IteratorAdvanced
- Fix the Async BugAdvanced
- Refactor to Async PatternAdvanced
All Python exercises · 50 practice problems · Advanced Python quiz