Skip to main content

Introduction to Concurrency, Threads & the GIL

By default, Python executes scripts synchronously line by line. If line 5 takes 10 seconds to download a file from the internet, line 6 is blocked and waits for 10 full seconds before executing.

Concurrency allows your program to juggle multiple tasks at once so that while one task is waiting for a network download, another task can continue processing.


1. Process vs. Thread: The Restaurant Metaphor

  • Process (A Complete Restaurant Branch): Has its own dedicated building, its own private kitchen, and its own separate electricity bill. (In computing: isolated memory space, heavy to start).
  • Thread (A Chef working inside the Kitchen): Multiple chefs share the exact same kitchen counter, knives, and refrigerator. (In computing: lightweight sub-worker sharing the same memory heap).
+-----------------------------------------------------------+
| Python Process (Dedicated OS Process & RAM Space) |
| ┌───────────────┐ ┌───────────────┐ ┌─────────────┐ |
| | Thread 1 (API)| | Thread 2 (UI) | | Thread 3(DB)| |
| └───────────────┘ └───────────────┘ └─────────────┘ |
+-----------------------------------------------------------+

2. I/O-Bound vs. CPU-Bound Tasks

Knowing when to use threading comes down to one question: Where is the program spending most of its time?

Task TypeBottleneckReal-World ExamplesBest Tool in Python
I/O-Bound (Input/Output)Network, Disk, Database (Waiting for external responses)Downloading 50 web pages, calling AI APIs, writing to diskthreading module
CPU-Bound (Calculation)Mathematical processing (Heavy calculations on CPU cores)Image filtering, matrix math, machine learning trainingmultiprocessing module

3. The Python GIL (Global Interpreter Lock) Explained

In CPython (the standard implementation of Python), there is a safety lock called the GIL (Global Interpreter Lock).

The GIL ensures that only one native thread executes Python bytecode at a time per process.

Why Does the GIL Exist?

Memory management in Python uses reference counting. The GIL prevents multiple threads from corrupting memory counters at the exact same millisecond.

Does the GIL Make Threading Useless?

No! For I/O-bound tasks (like network calls, database queries, and file downloads), Python automatically releases the GIL while waiting for the network card. This means 50 threads can download 50 files simultaneously in parallel!


Quick Summary

  • Concurrency vs. Parallelism: Concurrency is dealing with lots of things at once; parallelism is doing lots of things at once on multiple CPU cores.
  • Process vs. Thread: A process has its own isolated memory space; threads live inside a process and share the same memory heap.
  • Python's GIL: Restricts Python bytecode execution to 1 thread at a time, but releases automatically during I/O wait times (networking, disks).

What's Next?

Let's learn how to spawn background worker threads, pass arguments, and manage daemon lifecycles in Creating & Managing Threads!