Blocking code occupies the event-loop thread and delays other coroutines. Await syntax does not make synchronous work cooperative.
Before you start
You should know Python functions, collections and exceptions. Use a small isolated script or interactive session to trace the example. Pay attention to when objects are created and when work executes; iteration, binding and mutation can happen at different points in a program.
The practical goal is to reason through this situation: A synchronous file or network call inside a coroutine can stall unrelated tasks. Read the walkthrough first, then try the interview exercise before opening its answer. The important part is explaining the decision and its consequences, rather than remembering a definition alone.
Step-by-step walkthrough
Step 1: Identify actual suspension
Async syntax alone does not yield during a synchronous blocking function.
Step 2: Offload suitable I/O
Use an appropriate thread bridge for blocking APIs with bounded concurrency.
Step 3: Choose CPU strategy separately
CPU workloads may need processes or native-library-specific parallelism rather than unbounded thread offload.
Worked scenario
A synchronous file or network call inside a coroutine can stall unrelated tasks.
import asyncio
from pathlib import Path
async def read_text(path):
return await asyncio.to_thread(Path(path).read_text, encoding='utf-8')This avoids performing the synchronous read on the event-loop thread. Cancellation of the awaiting coroutine does not necessarily stop a thread function already running.
Common mistake
Marking a function async without suspension points does not parallelize it.
Verify the behavior
Run a periodic coroutine during a delayed read and compare responsiveness; verify the offload’s capacity and cancellation policy.
Interview exercise
Run a blocking operation safely.
Answer and reasoning
Use an appropriate thread offload for blocking I/O, bound concurrency and choose processes for suitable CPU workloads.
Continue learning
Compare the scenario with the Python interview questions and test your understanding with the Python MCQs. For terminology and implementation details, consult the reference material.