Fly with Python - Mission 7: Multitasking
Multitasking
Mission 7 opens Unit 3 with one of the most powerful ideas in real-world programming: doing many things at the same time. Students learn the StateMachine task scheduler, a built-in API that calls non-blocking functions on a periodic schedule. They build TaskMaster, a program that blinks LEDs at different intervals, schedules a one-time speaker beep, and uses non-blocking sensor checks to react when an object is near. The mission lays the foundation for every multi-tasking program in the rest of Unit 3.
Overview
Mission 7 kicks off Unit 3. Students learn how to escape sequential, blocking code by using a task scheduler. They create a StateMachine instance, schedule periodic tasks (like blinking LEDs at different intervals), schedule one-off delayed callbacks, and write non-blocking sensor checks so the drone can react to objects without freezing the rest of the program. The mission centers on one program, TaskMaster, and sets up the multitasking patterns used through the rest of Unit 3.
🎯 Mission Goal: Students will program a drone to perform multiple tasks at the same time without blocking program execution.
Learning Targets
- I can create an instance of the StateMachine.
- I can use the StateMachine module to schedule multiple tasks that run at the same time.
- I can write non-blocking code for sounds and sensors.
- I can have the drone respond without blocking when an object is near.
Key Concepts
- The task scheduler is like a super-efficient assistant for your code. It calls non-blocking functions periodically, at specified intervals. The periodic rate is in seconds.
- When scheduling a task, you don't call the function. You just give the name as an argument.
- Enabling debug will allow StateMachine to print to the console.
- Some blocking functions have options to make them non-blocking.
Assessment Opportunities
- Quiz after Objective 2
- Quiz after Objective 4
- Complete the program TaskMaster
- Mission 7 Assignment
- Mission 7 Review Questions
Success Criteria
- Use the task scheduler to blink an LED
- Use the task scheduler to blink two LEDs at different intervals
- Schedule a function to run one time
- Implement a non-blocking sensor check
- Have the drone respond when an object is near
Digital Resources
Classroom Materials
- ▸Laptop/computer with Chrome browser
- ▸CodeAIR drone and USB cable
Real-World Applications
sm.run_tasks() - checking input, updating physics, and rendering graphics on each tick.
Extensions & Cross-Curricular
select_index with the scheduler to launch different sets of tasks.
🔤
Vocabulary
▾
🐍
New Python Code
▾
sm.add_task(toggle_led9_task, 0.5)Schedule a task - use the name of the function only!
sm.schedule(speaker.off, 2.0)A scheduler function that runs a function just once after a specific delay - use the name of the function only.
get_data to a quick-check mode for a non-blocking function; returns None if no new data.
fwd, up, down = dataIf new data is found, unpack the tuple into separate variables for easier use.
📐
Standards
▾
CSTA Standards - Grades 6-8
CSTA Standards - Grades 9-10
CSTA Standards - Grades 11-12
- This mission is short (45-60 minutes) and stays mostly on-the-ground. No flight space is required for the core objectives.
- Review the StateMachine task scheduler concepts before class - students will reuse this pattern in every Unit 3 mission.
- Have CodeAIR drones charged and connected to laptops via USB.
-
Be ready to reinforce the difference between calling a function (
my_func()) and passing a function reference (my_func) - this is the most common student mistake in this mission.
- This is a fairly straight-forward and relatively short lesson. It uses concepts from earlier missions, like using the speaker, blinking LEDs, and reading the ranger sensors. It sets the tone for future missions, so don't go too fast and make sure students are clear on the concepts and code.
- Review questions can be used as a class review, made into a Kahoot!, or used to create an exam in your learning management system.
- Extensions and cross-curricular projects are included to enhance the concepts in the mission. You can use the extensions to extend students' programming experience. A remix is planned after Mission 10.
Lesson Outline
Students access prior knowledge by answering questions in the pre-mission section of the assignment doc.
Connect multitasking to real-world experiences through a brief discussion.
- Ask: "How does your phone play music while you're texting? How does a video game render graphics, check controller input, and update physics all at once?"
- Ask: "In Unit 2, we used
fly.steady()to make the drone hover. What was happening to the rest of our code while it ran?" - bring out the gridlock problem.
Front-load the key new programming concepts before students start coding.
- Introduce the StateMachine task scheduler - explain that it calls non-blocking functions periodically at intervals you set.
- Demonstrate the difference between calling a function (
my_func()) and passing a function as an argument (my_func) - this is critical for understandingsm.add_task(). - Preview the non-blocking versions of familiar functions:
speaker.beep(440, 0)for continuous tone,get_data(RANGERS, wait=False)for instant sensor read. - Set expectations: this is a 45-60 minute mission focused on one program, TaskMaster.
sm.enable_debug(True)) early - it gives helpful console feedback as tasks run.Students build the TaskMaster program, layering in scheduled tasks one at a time. As they work through the objectives, they should take notes and answer questions in their assignment doc.
-
Schedule a single task - create a StateMachine, define a callback function that toggles an LED, and use
sm.add_task()with an interval (Objectives 1-2; quiz after Objective 2). - Schedule multiple tasks at different intervals - add a second LED toggling at a different rate to show parallel execution.
-
One-time scheduling - use
sm.schedule()to start a continuous beep withspeaker.beep(440, 0), then schedulespeaker.offto fire once after a delay (Objective 3). -
Non-blocking sensor check - use
get_data(RANGERS, wait=False)inside a scheduled task; check forNonebefore unpacking (Objective 4; quiz after Objective 4). - React to a nearby object - combine the sensor check with an LED or sound response so the drone reacts the moment something gets close.
sm.add_task(my_callback(), 0.5) with parentheses. The parentheses call the function immediately and pass None to add_task. The correct form is sm.add_task(my_callback, 0.5) - no parentheses.sm.run_tasks() repeatedly - without it, no task ever fires.Bring the class together to consolidate multitasking ideas before moving to the next mission.
- Ask: "What's the difference between a blocking and a non-blocking function? Give an example of each from this mission."
- Ask: "Why don't we put parentheses on the function name when we add a task?"
- Ask: "What real-world systems use task schedulers like this?" - connect to the Real-World Applications section.
Students answer the reflection question in the assignment doc and then submit.
Use the Mission 7 Review Questions through a preferred method - class discussion, Kahoot!, or LMS quiz.