Python Tutorial 0/52 lessons ~6 min read Lesson 40

    Threads, Processes & GIL

    Python's GIL (Global Interpreter Lock) means threads can't run Python bytecode in parallel — use multiprocessing or concurrent.futures for CPU-bound work.

    Course progress0%
    Focus
    9 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    Python's GIL (Global Interpreter Lock) means threads can't run Python bytecode in parallel — use multiprocessing or concurrent.futures for CPU-bound work.

    Understanding the topic

    Core concepts to understand:

    • GIL = one Python instruction at a time per process.
    • Threads good for I/O, bad for CPU.
    • multiprocessing bypasses the GIL.
    • concurrent.futures is the high-level API.

    Syntax reference

    Visual flow / code:

    python
    from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
    # I/O-bound: threads
    with ThreadPoolExecutor(max_workers=8) as pool:
    results = list(pool.map(fetch_url, urls))
    # CPU-bound: processes
    with ProcessPoolExecutor() as pool:
    results = list(pool.map(heavy_compute, data))

    Execution workflow

    1Threads, Processes & GIL Workflow
    1 / 4

    GIL = one Python instruction…

    GIL = one Python instruction at a time per process.

    Real-world use

    Python 3.13 introduces an experimental no-GIL build (PEP 703). For now: threads for I/O, processes for CPU, async for thousands of connections.

    Best practices

    • Threads for I/O.
    • Processes for CPU.
    • Async for very-high-fanout I/O.

    Common mistakes

    • Trying to speed up NumPy with threads — usually works (releases GIL), but pure Python doesn't.

    Advanced interview questions

    Interview Prep

    Practice concise answers, then expand each card for the explanation.

    3 questions
    1IntermediateQuestionWhat is the GIL?+

    Answer

    The Global Interpreter Lock is a mutex in the default CPython build that lets only one thread execute Python bytecode at a time. It simplifies memory management, since reference counting is not thread-safe on its own. It is released during blocking I/O and by many C extensions such as NumPy, so threads still help I/O-bound work. It does not make your own compound operations atomic, so you still need locks.

    Follow-up

    Does the GIL make x += 1 thread-safe?
    2IntermediateQuestionThreads vs processes vs async — when?+

    Answer

    Use asyncio for large numbers of concurrent network I/O operations when libraries support it. Use threads for blocking I/O when you cannot use async, or for libraries that release the GIL. Use processes (multiprocessing or ProcessPoolExecutor) for CPU-bound pure-Python work. Measure first, because the right choice depends on the bottleneck and on how much data must be passed around.

    Follow-up

    What is the cost of sending data between processes?
    3AdvancedQuestionWill Python remove the GIL?+

    Answer

    Not by default, but it is becoming optional. PEP 703 added an experimental free-threaded build in Python 3.13, which you enable at build or install time, and later releases continue to mature it. The standard build keeps the GIL, and many C extensions need updating to be thread-safe without it. So the GIL is not simply removed; there are now two build variants.

    Follow-up

    What trade-offs does the free-threaded build have?

    Summary

    In summary: GIL serializes Python bytecode. Pick the right tool for the workload.

    Ready to mark this lesson complete?Track your journey across the entire course.