Concurrency: tasks, races and async lesson plan
Concurrency on one core in MicroPython on a Pico: superloops, interrupt races measured for real, critical sections, coroutines, asyncio, queues, and a responsive multi-sensor box.
Objectives and materials
By the end, students can
- From a blocking loop to a superloop, then interrupts and the race they bring.
- Critical sections, the no-memory rule of hard interrupts, and micropython.schedule.
- Generators as coroutines, a scheduler by hand, asyncio, and queues with backpressure.
- A multi-sensor box that never stops answering, with its lag measured.
Materials for the whole unit
- Clock
- LED
- Potentiometer
- Pushbutton
- Raspberry Pi Pico
- Resistor
Every part is already on the simulator’s bench, so there is nothing to buy or hand out. The counts are the most any one lesson uses, if you build it on real breadboards too.
Two things at once
From a blocking loop to a superloop, then interrupts and the race they bring.
| Lesson | Time | What students do |
|---|---|---|
| Doing two thingsWhy one sleep() ruins a two-job program, and the superloop that fixes it: timestamps, ticks_diff and a debounced button. | 40 min |
|
| Interrupt racesA hard interrupt counts a 1 kHz wheel sensor. Put a little work in the wrong place and ticks vanish: a real race, measured. | 40 min |
|
Materials
- Clock
- LED
- Potentiometer
- Pushbutton
- Raspberry Pi Pico
- Resistor
Check for understanding
Each student finishes "Interrupt races" on their own, without opening the hints. The simulator checks 5 of its 5 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Read, modify, write".
Sharing safely
Critical sections, the no-memory rule of hard interrupts, and micropython.schedule.
| Lesson | Time | What students do |
|---|---|---|
| Critical sectionsClose the race for good with disable_irq, measure how long interrupts stay off, then meet the rule hard interrupts live by: no new memory. | 40 min |
|
Materials
- Clock
- LED
- Potentiometer
- Pushbutton
- Raspberry Pi Pico
- Resistor
Check for understanding
Each student finishes "Critical sections" on their own, without opening the hints. The simulator checks 5 of its 5 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Schedule the heavy part".
Cooperative tasks
Generators as coroutines, a scheduler by hand, asyncio, and queues with backpressure.
| Lesson | Time | What students do |
|---|---|---|
| CoroutinesGenerators are functions that pause. Build a round-robin scheduler, then one that sleeps: the engine inside every async library. | 40 min |
|
| asyncio in MicroPythonTasks with async and await, the blocking mistake that freezes them all (measured), and a hard IRQ waking a task through ThreadSafeFlag. | 40 min |
|
| QueuesA ring buffer an interrupt can fill without allocating, what happens when the consumer is too slow, and a queue class with backpressure for tasks. | 40 min |
|
Materials
- Clock
- LED
- Potentiometer
- Pushbutton
- Raspberry Pi Pico
- Resistor
Check for understanding
Each student finishes "Queues" on their own, without opening the hints. The simulator checks 5 of its 5 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "A queue for tasks".
Boss
A multi-sensor box that never stops answering, with its lag measured.
| Lesson | Time | What students do |
|---|---|---|
| Boss: a responsive multi-sensor boxEverything at once: an IRQ-counted wheel with a critical section, a smoothed knob in slices, a flag-driven button, a heartbeat, an alarm and a lag watchdog. | 50 min |
|
Materials
- Clock
- LED
- Potentiometer
- Pushbutton
- Raspberry Pi Pico
- Resistor
Check for understanding
Each student finishes "Boss: a responsive multi-sensor box" on their own, without opening the hints. The simulator checks 6 of its 6 steps as they go.
Then ask each one to explain, in a sentence or two, the last thing it asked of them: "Your design review".
Teach this unit as a class
Start a class from this course and its 7 lessons are assigned in one go, in order. Students join with a six-character code or printed login cards, and each one’s work comes back to you.
The class tools are free for 30 days for one class of up to 20 students, with no card. The lessons themselves are free either way.