Command Palette

Search for a command to run...

Hectal
Back to the lesson: Topic 12.1 — Concurrency vs Parallelism: Threads, Processes, Async
Python · Example 2 of 3

A decision helper for the three models

A toy version of the decision table. Change a job's flags and predict the answer first.

Concurrency means several tasks are in progress at once, even if only one runs at any instant; parallelism means several really run at the same instant. Python gives you three tools (threads, processes and asyncio), and the right one depends on whether your program mostly waits (I/O-bound) or mostly calculates (CPU-bound).

Change the code and press Run (Ctrl+Enter). Predict what will happen first, then break it on purpose and read what changes. Your edits are saved and match the lesson page.

Ready for more? This phase has 6 hands-on exercises with automatic checks.

Practice questions

Write the code in the editor, run it, then open the model answer to compare.

01

Classify each as I/O-bound or CPU-bound: (a) downloading 50 PDFs, (b) computing the 35th Fibonacci number with recursion, (c) reading 10,000 rows from a database over the network.

02

Using asyncio.gather, run three coroutines a, b, c that sleep 0.03, 0.01 and 0.02 seconds, and print their names in the order they finish.

03

Write a function choose(job) that returns "asyncio" when job is "many api calls", "processes" when it is "heavy maths", and "no concurrency" otherwise.

Explain it without notes

01

What is the difference between concurrency and parallelism? Which does asyncio give you?

02

What is the difference between I/O-bound and CPU-bound work? Give one AI-related example of each.

03

Why is asyncio called cooperative, and what is the danger of that?

04

A script calls an LLM API 500 times, one after another, and takes ten minutes. What would you change, and why does it work?

A decision helper for the three models
Sign in to run this example in your browser.

Expected output

1000 LLM API calls                     -> asyncio
resize 500 photos with pure Python     -> processes (or NumPy)
read 20 files                          -> threads (or asyncio)
add up a list of 100 numbers           -> plain code: no concurrency needed