Python Interview Questions on Async Tasks and Cancellation
Calling an async function creates a coroutine object; it does not by itself schedule that coroutine as a concurrent task. Python's asyncio tools let you await work, schedule independent tasks and handle cancellation. The important interview question is who owns unfinished work when the operation…
By Ravindra Wadile · Published

559 words · About 3 min read
Calling an async function creates a coroutine object; it does not by itself schedule that coroutine as a concurrent task. Python's asyncio tools let you await work, schedule independent tasks and handle cancellation. The important interview question is who owns unfinished work when the operation stops.
The example uses Python 3.11 or later because it uses TaskGroup. It simulates waiting with asyncio; it does not make real service calls.
What is the difference between awaiting and scheduling?
An await waits for the awaited operation while allowing the event loop to run other ready work. If you await one independent lookup and only then create the second, you have not arranged for both lookups to start together. A task represents scheduled coroutine work.
Concurrency is not the same as executing Python CPU instructions on several cores. Explain the kind of waiting in your example. Async I/O is useful when the program can do something else while an operation waits; it is not a universal speed switch.
How can related tasks have a clear lifetime?
import asyncio
async def lookup(name, delay):
await asyncio.sleep(delay)
return name.upper()
async def combine():
async with asyncio.TaskGroup() as group:
first = group.create_task(lookup("north", 0.01))
second = group.create_task(lookup("south", 0.02))
return first.result(), second.result()
assert asyncio.run(combine()) == ("NORTH", "SOUTH")
The task-group boundary waits for its tasks before the function returns normally. It also defines related-task behavior when a child fails. Do not substitute a list of forgotten tasks and assume their errors or cleanup will be handled automatically.
What does cancellation mean?
A cancellation request asks a task to stop. It is normally delivered through a cancellation exception at an opportunity in the asynchronous execution. Code should perform needed cleanup and generally let cancellation continue. Swallowing it can make an operation look successful or prevent its owner from ending the task group as intended.
async def cancellation_example():
started = asyncio.Event()
cleaned = []
async def worker():
try:
started.set()
await asyncio.sleep(60)
finally:
cleaned.append("closed")
task = asyncio.create_task(worker())
await started.wait()
task.cancel()
try:
await task
except asyncio.CancelledError:
pass
assert cleaned == ["closed"]
asyncio.run(cancellation_example())
The event ensures the worker has started before cancellation is requested. Awaiting the cancelled task observes its completion and gives its finally block a chance to run. The long sleep is cancelled; the example does not wait for sixty seconds.
Does a timeout prove an operation had no effect?
No. An external service might complete a request even if your caller stops waiting. Local task cancellation cannot undo a remote side effect. An interview answer should separate the caller's timeout, the local task's cleanup and the external system's contract.
For a write request, discuss how the system identifies repeated operations and how the caller discovers an uncertain result. Do not promise that retrying after every timeout is safe.
What mistakes should you look for in a review?
- A coroutine is created but neither awaited nor scheduled.
- A task outlives the operation that needs its result.
- An exception is never observed.
- Cancellation is caught and reported as ordinary success.
- Blocking work prevents the event loop from reaching useful waiting points.
For practice, make one lookup fail and describe which result the caller receives. Consult the documented TaskGroup exception behavior for your Python version. Reason about ownership and cleanup before adding retry loops.
References and next practice
Browse interview topics · Practise a question in Interview Challenge · How to Answer Technical Interview Questions Out Loud