Practice — Concurrency vs Parallelism (5 questions)
Concurrency vs Parallelism
Define concurrency and parallelism precisely, and explain the relationship between them. Give one concrete example where a system is concurrent but not parallel.
Threads vs Processes
Compare threads and processes across memory, communication cost, and crash isolation. When would you deliberately choose multiple processes over multiple threads?
The GIL and Workload Choice
A Python service has two hot paths:
- A route that computes SHA-256 hashes over large in-memory buffers in a tight Python loop.
- A route that fans out to five external HTTP APIs and aggregates the responses.
For each path, would you use threads, asyncio, or multiprocessing? Justify
each answer in terms of the GIL.
Sharing One Async Session
The following async handler crashes under load with
InvalidRequestError: This session is provisioning a new connection; concurrent operations are not permitted:
a, b, c = await asyncio.gather(
get_top_subjects(db),
get_top_questions(db),
get_categories(db),
)
Explain the root cause in terms of concurrency, and give two different correct fixes.
How One Thread Serves Many Users
If an asyncio web server runs on a single thread with a single event loop,
how can it serve thousands of simultaneous users without blocking? What is
the eventual bottleneck for database-heavy endpoints?