Lesson 01
Threads Overview
Learn what a Python thread is, how MainThread differs from a child thread, and why a thread is one flow of control inside a program.
Learn what a Python thread is, how MainThread differs from a child thread, and why a thread is one flow of control inside a program.
What You Will Learn
- Say what a thread is without reading the whole repo
- Name MainThread vs a child thread
- Follow the five-lesson path in this module in order
Easy map (read this first)
You do not need to open every file in MahiDev. This module is only five short lessons:
| Step | Lesson | One sentence |
|---|---|---|
| 1 | Threads overview (here) | A thread is one flow; MainThread is the default. |
| 2 | Function threads | Point Thread at a function and call start(). |
| 3 | Class threads | Same idea with a method on a class. |
| 4 | Join and status | Wait with join() and inspect active_count(). |
| 5 | Thread locks | Use Lock when two flows share data. |
Kitchen analogy: one chef (MainThread) can prep while another chef (child) boils water. They are not two separate restaurants (that would be processes). Threads share the same kitchen.
See two flows side by side
Use the timeline below. Type a child name and a message, then press Show thread timeline.
Loading thread timeline…
Example
import threading
print(threading.current_thread().name)
print(threading.active_count())The name is MainThread. The count is 1, because no child thread has been started. Later lessons call Thread.start(). This browser playground cannot start a second operating-system thread, so those pages run the same function on MainThread and print that fact.
Key takeaways
- A thread is a flow of control
MainThreadis the default flow, created when the program starts- A child thread runs a function or a method
- One flow is sequential; extra child flows are the thread-based style
- A thread is lighter than starting a whole new process for the same small task
- Next: Function Threads — point
Threadat a function and callstart()
Loading playground…
Check your understanding
Threads Overview Quiz
A thread in this lesson is best described as what?
1 / 10