Skip to content

Interview PrepConcurrency · Advanced

Concurrency and asyncio ​

51. When should you use threading, multiprocessing or asyncio? ​

The short answer

Match the tool to what the work waits on. I/O-bound with many concurrent tasks, such as thousands of HTTP calls or connections: asyncio, one thread switching between tasks while each waits. I/O-bound with blocking libraries that have no async version: threads, since the GIL is released during I/O. CPU-bound pure-Python work: processes, each with its own interpreter, or threads on the free-threaded build. Mixing is common: an asyncio web server that sends heavy CPU work to a process pool.

asyncioThreadsProcesses
Runs onOne thread, cooperative switchingOS threads, preemptiveSeparate interpreters
Good forMany waiting tasks: network, DBBlocking I/O librariesCPU-heavy pure Python
CPU parallelismNoOnly with free-threading or C codeYes
Memory per taskTiny, a few KBAbout an OS thread stackA whole interpreter
Shared stateSafe between awaitsNeeds locksNot shared; copy or IPC
Typical number10,000+ tasksTens to hundredsOne per core

The GIL measurements behind this table are in the GIL question.

52. How does the asyncio event loop work, and what do async and await do? ​

The short answer

async def defines a coroutine function; calling it returns a coroutine object and runs nothing yet. await runs another coroutine and, whenever it must wait, such as for a network reply or a timer, pauses this coroutine and hands control back to the event loop, which runs other ready tasks meanwhile. When the awaited operation completes, the loop resumes the coroutine where it stopped. Only one task runs Python code at any instant, so concurrency comes from overlapping the waiting. asyncio.run(main()) starts the loop.

python
import asyncio, time

async def fetch(name: str, delay: float) -> str:
    await asyncio.sleep(delay)              # (1) stands in for a network call
    return f"{name} done"

async def main():
    t = time.perf_counter()
    await fetch("stock", 0.3)               # (2)
    await fetch("price", 0.2)
    print(round(time.perf_counter() - t, 1))          # 0.5

    t = time.perf_counter()
    results = await asyncio.gather(fetch("stock", 0.3), fetch("price", 0.2), fetch("reviews", 0.1))   # (3)
    print(results, round(time.perf_counter() - t, 1))
    # ['stock done', 'price done', 'reviews done'] 0.3

asyncio.run(main())
  1. await asyncio.sleep pauses without blocking the thread, so other tasks can run.
  2. Awaiting one after another adds the waits: 0.3 + 0.2 seconds.
  3. gather runs them concurrently: the total is the longest single wait, 0.3 seconds, not the sum of 0.6.

This is the same model as JavaScript's event loop and promises (the JavaScript event loop).

53. How do gather, TaskGroup and timeouts work in asyncio? ​

The short answer

asyncio.gather(*coros) runs coroutines concurrently and returns their results in order; by default the first exception propagates while the others keep running. asyncio.TaskGroup, since 3.11, is the safer structured form: tasks created in an async with block are all awaited at the end, and if one fails, the others are cancelled and every failure is raised together as an ExceptionGroup. asyncio.timeout(seconds) cancels whatever runs inside it if it takes too long, raising TimeoutError. A Semaphore limits how many run at once.

python
async def main():
    try:
        async with asyncio.timeout(0.15):                   # (1)
            await fetch("slow", 1)
    except TimeoutError:
        print("timed out")

    async def failing():
        await asyncio.sleep(0.05)
        raise ValueError("price service down")

    try:
        async with asyncio.TaskGroup() as tg:               # (2)
            stock = tg.create_task(fetch("stock", 0.5))
            tg.create_task(failing())
    except* ValueError as eg:                               # (3)
        print([str(e) for e in eg.exceptions], stock.cancelled())
        # ['price service down'] True

    limit = asyncio.Semaphore(2)                            # (4)
    async def call(i):
        async with limit:
            await fetch(f"page {i}", 0.05)
    await asyncio.gather(*(call(i) for i in range(10)))     # at most 2 at a time
  1. A deadline for the block. The slow call is cancelled after 0.15 seconds.
  2. A task group owns its tasks: the block does not exit until all finish or fail.
  3. One failure cancels the rest and arrives as an exception group (except*).
  4. A semaphore caps concurrency, for example to respect a partner's rate limit.

54. What happens if you call blocking code inside an async function? ​

The short answer

It freezes the whole event loop. A blocking call, such as time.sleep, requests.get, a synchronous database driver, or heavy computation, holds the only thread, so no other task runs until it finishes, and every concurrent request stalls. Use async libraries instead, such as httpx.AsyncClient, asyncpg or async SQLAlchemy; move unavoidable blocking calls to a thread with await asyncio.to_thread(func); and send CPU-heavy work to a process pool. In FastAPI, a plain def endpoint runs in a thread pool for exactly this reason.

python
import time

async def bad():
    time.sleep(0.3)                  

await asyncio.gather(bad(), bad())   # takes 0.6 s: they ran one after the other

def render_pdf():
    time.sleep(0.3)                  # a blocking library call
    return "pdf"

results = await asyncio.gather(asyncio.to_thread(render_pdf), fetch("x", 0.3))   # (1)
# ['pdf', 'x done'] in 0.3 s
  1. to_thread runs the blocking function in a worker thread, so the loop keeps serving other tasks, and both finish together.

Spotting it in production: asyncio.run(main(), debug=True) warns about tasks that block the loop for over 100 ms, and event-loop lag metrics show it.

55. What are concurrent.futures executors? ​

The short answer

concurrent.futures gives one simple interface over pools of workers. ThreadPoolExecutor runs functions in threads, good for blocking I/O. ProcessPoolExecutor runs them in separate processes, good for CPU-bound work, with arguments and results pickled between processes. InterpreterPoolExecutor, new in 3.14, uses subinterpreters. You call submit(fn, *args) to get a future, or map(fn, items) for many inputs, and use as_completed to handle results as they finish. Use them inside with so the pool shuts down cleanly.

python
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor, as_completed

def resize(path: str) -> str: ...            # CPU heavy
def download(url: str) -> bytes: ...         # blocking I/O

if __name__ == "__main__":                   # (1)
    with ProcessPoolExecutor() as pool:
        thumbs = list(pool.map(resize, image_paths))

    with ThreadPoolExecutor(max_workers=20) as pool:
        futures = {pool.submit(download, u): u for u in urls}
        for fut in as_completed(futures):    # (2)
            print(futures[fut], len(fut.result()))
  1. The main guard is required for process pools on Windows and macOS, where child processes re-import the script.
  2. Results in completion order, not submission order.

From asyncio, use loop.run_in_executor(pool, fn, arg) to await a pool's work.

56. How do race conditions happen in Python, and how do you prevent them? ​

The short answer

A race condition happens when several threads read and modify shared state and the result depends on timing. Even with the GIL, balance = balance + 1 is several bytecode steps, and a thread can be switched out between reading and writing, so updates get lost. Prevent it by protecting the shared state with a threading.Lock around the read-modify-write, by not sharing, giving each thread its own data and combining results at the end, or by passing work through a thread-safe queue.Queue. On free-threaded Python, unprotected races happen much more often, so this matters more than ever.

python
import threading, time

balance = 0
def deposit(times):
    global balance
    for _ in range(times):
        current = balance          # (1)
        time.sleep(0)              # (2)
        balance = current + 1

threads = [threading.Thread(target=deposit, args=(1000,)) for _ in range(4)]
for t in threads: t.start()
for t in threads: t.join()
print(balance)                     # far below 4000; 1303 in one run      (3)

lock = threading.Lock()
def deposit_safe(times):
    global balance
    for _ in range(times):
        with lock:                 # (4)
            current = balance
            time.sleep(0)
            balance = current + 1
# with the lock: always 4000
  1. Read the shared value.
  2. Yield the thread on purpose, which makes the race show up reliably; in real code a log call or I/O does the same.
  3. Lost updates: two threads read the same value and both write current + 1.
  4. The lock makes read and write one indivisible step for other threads.

asyncio has the same problem across await: if a coroutine reads a value, awaits, then writes, another task can change it in between. Use asyncio.Lock there.

57. What are subinterpreters, and how do they differ from threads and processes? ​

The short answer

A subinterpreter is a separate, isolated Python interpreter running inside the same process, each with its own modules, globals and, since 3.12, its own GIL. Python 3.14 made them usable from Python code through the concurrent.interpreters module and InterpreterPoolExecutor. They give real parallelism like processes, with lower start-up cost and memory, but data is not shared freely: objects are passed by copying or through special channels. Many C extensions do not support them yet. They are an option between threads and processes, not yet a default choice.

ThreadsSubinterpretersProcesses
Parallel CPU with standard CPythonNoYesYes
Shared Python objectsYesNo, copiedNo, pickled
Start-up costLowestLowHighest
Extension supportAllGrowingAll
Available asthreadingconcurrent.interpreters, 3.14+multiprocessing

Combine chapters into one PDF

Pick the chapters you want. They print in the order you pick them.

Interview Prep
Python
Foundations
Functions
Iteration
Objects
Internals
Errors and packaging
Concurrency
Modern Python
Coding
Other topics
Nothing picked yet