Understanding Processes, Threads, and the GIL in Python
This article explains the concepts of processes and threads, describes Python's Global Interpreter Lock (GIL) and its impact on concurrency, compares the low‑level _thread module with the higher‑level threading module, and provides example code illustrating thread creation, synchronization with locks, and common pitfalls.
What is a process?
A process is a program in execution.
It has its own address space, memory, data stack, etc.
The operating system manages processes uniformly.
New processes can be created via fork or spawn.
Inter‑process communication (IPC) allows processes to share information.
What is a thread?
A thread runs within a process and shares the same context.
Threads can share information more easily than processes.
Multiple threads can execute concurrently.
Synchronization primitives are required to avoid race conditions.
Python and threads
The interpreter runs a main loop where only one thread executes Python bytecode at a time.
This is enforced by the Global Interpreter Lock (GIL).
The GIL
The GIL is set before a thread runs.
When the thread finishes a slice of bytecode or voluntarily yields, the GIL is released.
The thread may then be put to sleep, and the GIL is unlocked for another thread.
These steps repeat continuously.
Two thread‑management modules in Python
_thread : low‑level module providing basic thread and lock primitives.
threading : higher‑level, feature‑rich module built on top of _thread , offering locks, daemon threads, and more.
_thread module examples
Example 1 demonstrates creating a thread with _thread.start_new_thread and shows that the main program must stay alive (e.g., using time.sleep ) otherwise child threads are terminated.
Example 2 adds a lock to coordinate the main and child threads, ensuring the child finishes before the program exits.
threading module examples
The threading module provides a built‑in lock, simplifying the previous lock example. The code creates a threading.Thread object, starts it, and joins it.
A note warns that the execution order of the threads (e.g., “loop 0” and “loop 1”) is nondeterministic.
Thread class and subclassing
By subclassing threading.Thread , one can encapsulate thread behavior in an object‑oriented way, overriding the run method and using instance attributes for communication.
Python Programming Learning Circle
A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.