Skip to content
Tech Interview Prep
Technical interview guide

Concurrency (GIL, Threading, Asyncio)

Why Python threads don't parallelize CPU work, and the two real ways around it: multiprocessing and asyncio.

Read
48 min
Practice MCQs
25
Interview Q&A
25
Edition
v2
Editorial status
Reviewed

Scope: Python 3.14, covering default GIL-enabled CPython and explicitly optional free-threaded builds.

Curated: · Written: · Reviewed:

25 questions

MCQ-1A batch job runs a pure-Python CPU-bound scoring loop on 8 threads under standard CPython 3.12. Wall time matches the single-threaded run and only one core is busy. The algorithm cannot change. Which change is most likely to cut the wall time?
MCQ-2A CPython 3.12 service (default GIL build) keeps per-key counters in a plain dict. Eight worker threads each run `if counts[k] < LIMIT: counts[k] += 1`. Under load `counts[k]` reaches LIMIT + 3. Which fix is correct?
MCQ-6A Python 3.12 service guards a cache with `threading.Lock`. `refresh()` acquires the lock and then calls `evict()`, which acquires the same lock; under a load test with 8 worker threads the workers hang. Swapping in `threading.RLock` clears the hang, and the cache is still never read by another thread while `evict()` runs. Which statement describes the resulting behavior?
MCQ-7Why must a condition-variable waiter recheck its predicate after notification?
MCQ-23A service runs on a 4-core box. One handler does a pure-Python CPU-bound job: a 40 MB payload checksum that takes ~120 ms of bytecode execution, no C extension, no I/O: wrapped in `await asyncio.to_thread(checksum, payload)`. With 8 concurrent requests the loop stays responsive, but the 8 requests finish in ~960 ms wall clock, the same as running them one after another, and `top` shows one core pinned at 100% with the other three idle. What explains the 960 ms?
MCQ-24A team migrates a service to the free-threaded CPython 3.14 build (`python3.14t`) so its eight worker threads run in parallel. The workers share one plain `dict` of per-key counters and each runs `counts[k] += 1`; under the default build that line was wrapped in a `threading.Lock`, and on the free-threaded build the team removes the lock, expecting the GIL's removal to make the update safe. Counters still drift. Which statement explains why?
MCQ-3A team moves a CPU-bound service to CPython 3.14 on a 4-vCPU Linux container and keeps its start command, `python3.14 app.py`, pointing at the default build rather than the free-threaded one. Four worker threads each run a CPU-bound loop that mutates a large in-process cache, which cannot be moved behind IPC, and the four loops finish in about the same wall time as running them one after another. Which account of the measurement is right?
MCQ-4A Python 3.12 service on a 4-vCPU host runs two ThreadPoolExecutor pools, each with max_workers=8. A Pool A task blocks 200 ms on a socket read, then spends 2 ms parsing the reply in Python. A Pool B task spends 200 ms parsing a large JSON payload in pure Python, then 2 ms on the socket. The team raises both pools to max_workers=32. Which outcome should they expect?
MCQ-8A fixed-size ThreadPoolExecutor has all of its worker threads running tasks that each call `.result()` on a Future for another task submitted to the same pool. What occurs?
MCQ-5A thread-pool worker holds a plain result value; a coroutine on the running event loop is awaiting an `asyncio.Future` that must be resolved with it. Which handoff is correct?
MCQ-9A Python 3.12 service on Linux (default fork start method) passes a plain list to a multiprocessing.Process target that appends one result to it. After p.join(), the parent's list is still empty. Which explanation is correct?
MCQ-11A driver already holds 4,000 one-megabyte tiles in RAM and fans them out to an 8-process multiprocessing.Pool; each worker unpickles one tile, runs a 2 ms regex scan, and returns a small integer. The same 4,000 scans in one process take 8 s. Measured on this machine, one core running pickle.dumps plus the pipe write sustains about 250 MB/s for a 1 MB tile, and the pool forks its 8 workers. The team wants the pool's wall clock predicted before changing any code. Which prediction is right?
MCQ-12A ProcessPoolExecutor is created with max_workers=4 and handed four long-running tasks. Each task calls executor.submit() on that same executor and then blocks on the returned future's result(). What is the documented outcome?
MCQ-10A rules-matching job runs `multiprocessing.Pool(8)` over 20,000 records. On the Linux dev box (Python 3.12, default start method) it finishes in 55 s and matches 1,842 records. On the macOS build machine the same file exits immediately with `RuntimeError: An attempt has been made to start a new process before the current process has finished its bootstrapping phase`. The rules live in a module-level `_RULES` dict that `main()` fills before the pool is created, and the parent process also runs a threaded health-check server, so the team has standardized on spawn. What change makes the job produce 1,842 matches on both machines while still running the matching across 8 processes?
MCQ-13```python import asyncio async def refresh(): print("start") await asyncio.sleep(1) print("done") coro = refresh() print("after") ``` Running this prints `after` and nothing else. What does the call `refresh()` actually do?
MCQ-14A single-loop asyncio service keeps per-account balances in a plain module-level dict. Each handler runs `bal = balances[acct]`, then `await fetch_fee(acct)` (suspends 2-5 ms), then `balances[acct] = bal - amount`, with no lock. Under 200 concurrent requests over 40 accounts, balances drift and debits are silently lost, though the process is single-threaded with no threads. Which statement explains the lost update and names the minimal correct fix?
MCQ-15A Python 3.11 service handles about 200 requests/s. Each handler ends with `asyncio.create_task(write_audit(entry))` and returns the response without keeping the returned object. Under sustained load roughly 1 in 10,000 audit rows never reaches the database, and the asyncio logger occasionally prints 'Task was destroyed but it is pending!'. What is the cause and the fix?
MCQ-17A worker task is suspended in `await asyncio.sleep(30)` inside a `try/finally` whose `finally` does `await conn.close()`. A supervisor calls `task.cancel()` once. What happens to the task?
MCQ-16A service queries three replicas concurrently inside a single `async with asyncio.TaskGroup()` block. One child raises `ConnectionError` while the other two are still awaiting their responses. What does the TaskGroup do?
MCQ-20A report job fans out three independent queries and must emit rows in the fixed order A, B, C no matter which query finishes first. A failure in one query must not cancel the other two, and the job needs all three outcomes before it decides what to write. Which construct matches?
MCQ-19A service stores `self.checkpoint_task = asyncio.create_task(write_checkpoint())`, and a request handler runs `await asyncio.shield(self.checkpoint_task)`. `write_checkpoint()` takes 300 ms. At t=100 ms the client disconnects and the framework cancels the handler task; the handler does not catch CancelledError, and the process keeps running. What happens?
MCQ-18A payments service runs `await asyncio.wait_for(charge(order), timeout=2.0)`. At 2.0 s the coroutine raises `TimeoutError` and the client never sees a response. The team wants to know whether it can retry the charge. What does the timeout actually establish?
MCQ-21A single-loop asyncio ingest service has one producer coroutine that reads an endless HTTP stream and calls `await queue.put(event)`, and 40 consumer coroutines that each `await queue.get()` and then await a downstream call taking about 50 ms. The queue is constructed without a maxsize, and during a 10x burst resident memory grows until the OOM killer fires. Events must not be lost, and consumers must be allowed to finish the work they have started. Which change bounds memory without losing events?
MCQ-22A service keeps a shared `dict` of per-connection counters. Coroutines update it under a module-level `asyncio.Lock`; blocking work is offloaded with `asyncio.to_thread`, and those worker functions mutate the same dict with no lock. Under load the counters drift and iteration occasionally raises `RuntimeError: dictionary changed size during iteration`. What is the correct fix?
MCQ-25A Python 3.12 batch job processes 64 items on an 8-vCPU host. Each item needs 120 ms of pure-Python CPU work (no C extension releases the GIL), then a 30 ms HTTP call to a metadata service issued with aiohttp. The job runs on one asyncio event loop, and the CPU function is called inline in the coroutine. It takes 9.4 s; the team needs it under 2 s. Which change gets it there?