Interview Q&A · Python · 25 questions in this topic

Concurrency & Parallelism — Python Interview Q&A

All Python topics

Threads, processes and asyncio — what each one is actually good at, what the GIL prevents, and how to choose under a real workload. Part of Concurrency & Parallelism · 375 questions in the Python bank.

What is the difference between a process and a thread in Python?

Answer

A process is an independent program with its own memory space; threads are lighter-weight units of execution that run within a process and share that process's memory. In Python specifically, threads within one process are also constrained by the GIL (Q233), which limits their ability to run Python bytecode truly simultaneously.

Because threads share memory, communication between them is simple (shared variables) but requires careful synchronization to avoid race conditions (Q241). Because processes have separate memory, communication requires explicit inter-process communication (Q240), but each process gets its own independent GIL, enabling genuine parallel execution of Python code.

Example

Example.pypython
import threading
import multiprocessing
def worker():
    print("Working...")
# Thread: shares memory with the main process
t = threading.Thread(target=worker)
t.start()
t.join()
# Process: separate memory space entirely
p = multiprocessing.Process(target=worker)
p.start()
p.join()

💡 Immediately connect this to the GIL — "threads share memory but are limited by the GIL for CPU-bound work; processes have overhead but truly parallelize" is the one-sentence summary that sets up nearly every other question in this section.

What is the threading module, and how do you create a thread?

Answer

threading is the standard library module for creating and managing threads. You create a Thread object with a target function, call.start() to begin execution, and.join() to wait for it to finish.

.join() is essential when the main program needs to wait for background threads to complete before continuing (e.g., before the program exits) — without it, the main thread might finish and the program could terminate while background threads are still running.

Example

Example.pypython
import threading
import time
def download_file(name, delay):
    print(f"Starting download: {name}")
    time.sleep(delay) # simulates an I/O-bound wait
    print(f"Finished download: {name}")
threads = []
for name, delay in [("file1", 2), ("file2", 1), ("file3", 3)]:
    t = threading.Thread(target=download_file, args=(name, delay))
    threads.append(t)
    t.start()
for t in threads:
    t.join() # wait for all threads to finish before continuing
print("All downloads complete")

💡 This exact "simulate concurrent downloads with time.sleep() " pattern is a very common way interviewers ask you to demonstrate basic threading — practice writing it fluently, including the.join() loop.

What is the Global Interpreter Lock (GIL)?

Answer

The GIL is a mutex (lock) in CPython that allows only one thread to execute Python bytecode at a time, even on a multi-core machine. This means true parallel execution of Python code across threads within a single process isn't possible in CPython — it's specific to the CPython implementation, not a fundamental property of the Python language itself.

The GIL exists primarily to simplify CPython's memory management (reference counting isn't thread-safe without some form of locking) and to make the interpreter itself easier and safer to implement — the trade-off is that CPUbound multi-threaded Python code doesn't get the speedup you'd expect from multiple cores.

Example

Example.pypython
import threading
import time
def cpu_bound_work():
    count = 0
    for _ in range(50_000_000):
        count += 1
start = time.perf_counter()
t1 = threading.Thread(target=cpu_bound_work)
t2 = threading.Thread(target=cpu_bound_work)
t1.start(); t2.start()
t1.join(); t2.join()
print(f"Two threads: {time.perf_counter() - start:.2f}s")
# Often NOT meaningfully faster than running cpu_bound_work() twice sequentially,
# because the GIL prevents true simultaneous execution of Python bytecode

💡 Explicitly say "this is a CPython implementation detail, not a Python language requirement" — other implementations (like Jython) don't have a GIL, and this distinction is exactly the kind of precision interviewers listen for.

Recommended resourcesAffiliate links · may earn us a commission

Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits