Trace Sequential and Concurrent Dependencies
Core idea
Sequential awaits wait for one operation before starting the next. Independent operations can be scheduled together so they overlap at suspension points; this is concurrency, not necessarily parallel CPU execution. Model dependencies explicitly: a dependent operation needs the earlier result, while independent work can start together.
import asyncio
async def main(): async def fetch_count(): return 4 async def summarize(n): return n*2 count = await fetch_count() print(await summarize(count))
asyncio.run(main())summarize depends on count.
Expected output:
8Start both independent jobs
import asyncio
async def main(): started = [asyncio.Event(), asyncio.Event()] release = asyncio.Event() async def job(i): started[i].set() await release.wait() return i+1 a = asyncio.create_task(job(0)) b = asyncio.create_task(job(1)) await started[0].wait() await started[1].wait() print('both started') release.set() print(await a + await b)
asyncio.run(main())Both jobs reach the gate before either can finish; events prove overlap without elapsed-time guesses.
Expected output:
both started3Common mistake
Creating two coroutine objects alone does not schedule them. Do not infer completion order from scheduling order; synchronize on explicit conditions.
Practice
Add a third independent job and a third started event; release only after all have started.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Sequential versus concurrent awaits
Check your understanding of sequential versus concurrent awaits.
What is printed?
import asyncio
async def main(): async def f(n): return n*2 x=await f(2) print(await f(x))
asyncio.run(main())What is printed?
import asyncio
async def main(): e=asyncio.Event() e.set() print(await e.wait())
asyncio.run(main())Which jobs are candidates for concurrent scheduling?
What establishes that both jobs reached a gate?