GCGrowCarrer

Concurrency and asyncio

Locked

Lesson 25 of 60 · Concurrency · Challenging

Finish these first:Iterators and Generators →Exceptions →Or pass the tests below to unlock it now.

1

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.
Key words (tap to learn):
2

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())
3

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?

4

Your challenge

2 sample tests, 1 hidden

Write async def gather_squares(xs) returning squares concurrently via asyncio.gather.

Stuck? Get a hint (free)
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.

Mastery0%

Learn this first

Opens up next

This is the last lesson in this branch.