Concurrency & Multithreading Flashcards
7 cards from real POC practice questions. Tap to flip, then mark Knew It or Still Learning — missed cards come back until you master them.
Read the first 7 Concurrency & Multithreading flashcards as text
Which class in the `concurrent.futures` module is best suited for thread-based parallelism?
Answer: ThreadPoolExecutor
ThreadPoolExecutor manages a pool of worker threads and provides a high-level interface for submitting tasks and retrieving results via Future objects.
What is the primary purpose of a threading.Lock object in Python?
Answer: To synchronize access to shared resources, preventing simultaneous modification by multiple threads
A Lock ensures mutual exclusion — only one thread can acquire the lock at a time, preventing race conditions on shared data.
In Python's asyncio framework, which keyword suspends a coroutine until an awaitable completes?
Answer: await
The `await` keyword suspends the current coroutine and yields control back to the event loop, which resumes it when the awaitable is done.
What is a deadlock in a multithreaded Python program?
Answer: A situation where two or more threads wait indefinitely for locks held by each other
Deadlock occurs when Thread A holds Lock 1 and waits for Lock 2, while Thread B holds Lock 2 and waits for Lock 1 — both block forever.
Which method of threading.Thread should you override in a subclass to define the thread's work?
Answer: run()
Overriding run() defines the code executed in the new thread; start() internally calls run() in a new thread context.
What is the primary advantage of asyncio over threading for I/O-bound tasks in Python?
Answer: asyncio uses cooperative multitasking, eliminating thread-switching overhead and scaling to thousands of concurrent tasks
asyncio's event loop runs in a single thread using cooperative multitasking — coroutines yield control voluntarily, avoiding the overhead of OS thread switching.
Which module and class should be used to safely pass data between threads using a FIFO queue?
Answer: queue.Queue
queue.Queue is thread-safe by design, using internal locks to prevent race conditions when multiple threads produce and consume items.