Concurrency and asyncio
LockedLesson 25 of 60 · Concurrency · Challenging
Finish these first:Iterators and Generators →Exceptions →Or pass the tests below to unlock it now.
The big idea
Python offers threads (I/O-bound, limited by the GIL for CPU work), processes (CPU-bound), and asyncio (cooperative I/O concurrency).
Think of it like this: Think of concurrency like multiple chefs sharing one kitchen: coordinating who uses the stove prevents two chefs from overwriting the same dish at the same moment.
Read the lesson
Python offers threads (I/O-bound, limited by the GIL for CPU work), processes (CPU-bound), and asyncio (cooperative I/O concurrency).
import asyncio
async def fetch(n):
await asyncio.sleep(0.1)
return n
async def main():
return await asyncio.gather(fetch(1), fetch(2))
asyncio.run(main())
Quick check
Before writing code: What should `__import__('asyncio').run(gather_squares([1,2,3]))` return when your implementation of Concurrency and asyncio is working?
Your challenge
Write async def gather_squares(xs) returning squares concurrently via asyncio.gather.
More learning tools(compare languages, visual guide, related lessons)
Compare with another language
Similar to java.concurrency-threads (40% overlap). Python asyncio is cooperative; Java uses threads/executors (virtual threads for I/O).
Visual walkthrough
A diagram, a step-by-step animation, and short captions for Concurrency and asyncio.
Learn this first
- Iterators and GeneratorsNot yet
- ExceptionsNot yet
Opens up next
This is the last lesson in this branch.