Last Updated: December 6, 2025
28 quizzes
Complete the expression so it creates a Thread object that will run the function handle_request.
worker = threading.Thread(target=)Click an option to fill the blank:
Complete the expression so it safely acquires a lock using a context manager before updating a shared counter.
with counter_lock.: shared_counter += 1Click an option to fill the blank:
Complete the expression so it schedules the coroutine fetch_page(url) as a new asyncio Task.
task = asyncio.(fetch_page(url))Click an option to fill the blank:
Which scenario benefits most from using threads instead of running tasks sequentially?
In Python's threading module, what does thread.join() do?
What is the main purpose of a threading.Lock?
Which condition is required for a deadlock to occur?
Why can the multiprocessing module bypass the limitations of the GIL for CPU-bound work?
What is a key difference between ThreadPoolExecutor and ProcessPoolExecutor?
In asyncio, what does the 'await' keyword do?
What is the role of the asyncio event loop?
What does asyncio.create_task() return?
Which statement about the Global Interpreter Lock (GIL) in CPython is correct?
Order the steps to safely increment a shared counter from multiple threads using a Lock.
Drag and drop to reorder, or use the arrows.
Order the steps to concurrently fetch two URLs using asyncio coroutines.
Drag and drop to reorder, or use the arrows.
What is the output of this code using a simple Lock for synchronization?
1import threading
2
3value = 0
4lock = threading.Lock()
5
6with lock:
7 value += 5
8
9print(value)What is the output when using concurrent.futures with a ProcessPoolExecutor?
1from concurrent.futures import ProcessPoolExecutor
2
3def square(x):
4 return x * x
5
6with ProcessPoolExecutor(max_workers=2) as executor:
7 future = executor.submit(square, 4)
8
9print(future.result())What is printed when running this asyncio code?
1import asyncio
2
3async def greet():
4 await asyncio.sleep(0)
5 return "hello"
6
7result = asyncio.run(greet())
8print(result.upper())What is the output of this code illustrating the GIL's effect on simple threading?
1import threading
2
3counter = 0
4
5def add_one():
6 global counter
7 counter += 1
8
9thread = threading.Thread(target=add_one)
10thread.start()
11thread.join()
12
13print(counter)What will this multiprocessing example print?
1import multiprocessing
2
3def get_name():
4 return multiprocessing.current_process().name
5
6if __name__ == "__main__":
7 with multiprocessing.Pool(processes=1) as pool:
8 name = pool.apply(get_name)
9 print(isinstance(name, str))Find the bug in this code that tries to protect a shared counter with a Lock.
Click on the line(s) that contain the bug.
import threading shared_counter = 0lock = threading.Lock() def increment_many(times): global shared_counter for _ in range(times): lock.acquire shared_counter += 1 lock.release() Find the bug in this asyncio code that creates tasks for coroutines.
Click on the line(s) that contain the bug.
import asyncio async def do_work(): await asyncio.sleep(1) return "done" async def main(): task = asyncio.create_task(do_work) result = await task print(result) asyncio.run(main()) Match each concurrency tool with its best description.
Click an item on the left, then click its match on the right. Click a matched item to unmatch.
Match each concept with the concurrency model it belongs to.
Click an item on the left, then click its match on the right. Click a matched item to unmatch.
Complete the code to use a Process Pool for CPU-bound work and collect all results.
import multiprocessingdef compute_square(number): return number * numberif __name__ == "__main__": with multiprocessing.(processes=4) as pool: results = pool.(compute_square, range(5)) print(list(results))Click an option to fill blank 1:
Complete the asyncio code so two coroutines run concurrently and both results are printed.
import asyncioasync def fetch_data(delay): await asyncio.sleep(delay) return f"waited {delay}"async def main(): task_one = asyncio.(fetch_data(1)) task_two = asyncio.(fetch_data(2)) result_one, result_two = await asyncio.(task_one, task_two) print(result_one, result_two)asyncio.run(main())Click an option to fill blank 1:
Click the line that can cause a deadlock when two threads run acquire_locks() concurrently.
Click on the line to select.
import threading lock_a = threading.Lock()lock_b = threading.Lock() def acquire_locks(): with lock_a: with lock_b: print("Both locks acquired") Click the line where using blocking I/O inside an async function can block the event loop.
Click on the line to select.
import asyncioimport time async def slow_operation(): time.sleep(2) return "done" async def main(): result = await slow_operation() print(result) asyncio.run(main())