Loading Page Content...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Preparing courses, live labs, and learning resources.सामग्री लोड हो रही है, कृपया प्रतीक्षा करें...
Concurrency is the composition of independently executing computations. In Python, the threading module provides high-level primitives for creating and managing native operating system threads. Understanding how Python threads interact with system hardware and the CPython runtime is essential for designing high-performance, responsive applications.
In CPython (the standard Python reference implementation), a threading.Thread object corresponds to a real, native operating system thread (e.g. a POSIX thread on Linux/macOS or a Win32 thread on Windows).
However, CPython relies on the Global Interpreter Lock (GIL)—a mutual exclusion mutex that prevents multiple native threads from executing Python bytecode simultaneously on separate CPU cores:
multiprocessing instead).Python supports two distinct architectural patterns for thread instantiation:
The most common approach: passing a target function and its arguments to threading.Thread:
threading.ThreadIdeal for encapsulating thread-specific state, telemetry, or custom lifecycle hooks:
join()The join([timeout]) method blocks the calling thread (usually the main thread) until the target thread terminates or the optional timeout expires:
Here is a practical benchmark illustrating concurrency gains when downloading mock web endpoints:
| Feature | Details |
|---|---|
| Underlying Type | Native OS thread (POSIX / Win32) |
| GIL Impact | Only one thread runs Python bytecode at any moment |
| Best Workloads | I/O-bound operations (networking, disk, database) |
| Subclassing Hook | Override the run() method |
| Execution Trigger | thread.start() (never call run() directly!) |
| Completion Await | thread.join([timeout]) |
Due to CPython's Global Interpreter Lock (GIL), multi-threaded CPU-bound calculations will not run faster on multi-core processors. Use multiprocessing for CPU-bound tasks and threading for I/O-bound tasks.
Even simple operations like counter += 1 compile into multiple bytecode instructions (LOAD_FAST, INPLACE_ADD, STORE_FAST) that can be interrupted by thread context switching. Always synchronize with a Lock.
Use Python's threading.Thread to check the responsiveness of multiple web hostnames concurrently, measuring total execution time vs sequential execution.
What is the primary constraint imposed by the Global Interpreter Lock (GIL) on Python threads in CPython? A. Python programs cannot spawn more than 10 threads. B. Only one native thread can execute Python bytecode at any single instant, even on multi-core processors. C. Threads cannot open files. D. Threads cannot access variables in the main thread.
Why does multithreading in Python dramatically speed up I/O-bound workloads despite the GIL? A. The GIL is permanently deleted during I/O operations. B. When a thread performs I/O operations (such as waiting for network or disk), CPython releases the GIL, allowing other threads to run concurrently. C. CPython converts I/O operations into GPU kernels. D. The operating system pauses all other processes.
What method should you call to begin concurrent execution of a threading.Thread instance? A. thread.run() B. thread.start() C. thread.execute() D. thread.spawn()
.start() instructs the OS to allocate and launch a native thread that executes run(). Calling .run() directly simply executes the function synchronously inside the current thread.What is the purpose of the thread.join() method? A. It terminates the target thread immediately. B. It merges the memory address spaces of two threads. C. It blocks the calling thread until the target thread finishes execution. D. It pauses the thread until user input is received.
join() pauses the calling thread (often the main application thread) until the thread upon which it is called has finished its execution.When subclassing threading.Thread, which method must be overridden to define the thread's background execution logic? A. __call__() B. execute() C. run() D. main()
threading.Thread, the run() method contains the code that will be executed once the thread is started via .start().In the next lesson, we will continue your advanced Python journey with Thread Synchronization (6: Multithreading in Python).
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Project: Stream Data Processor | Thread Synchronization |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.