Python Tutorial 0/52 lessons ~6 min read Lesson 39
async / await
async / await in Advanced Python: core concepts, worked examples, and practical guidance in one focused lesson.
Course progress0%
Focus
9 guided sections
Practice signal
Examples included
Career prep
Interview Q&A included
Introduction
async/await enables single-thread concurrency for I/O-bound code — perfect for web servers, scrapers, and pipelines doing many network calls.
Understanding the topic
Core concepts to understand:
async defdefines a coroutine.awaityields control to the loop.asyncio.gather()runs in parallel.- Blocking calls freeze the loop — use async libs.
Syntax reference
Visual flow / code:
python
import asyncio, httpxasync def fetch(client, url):r = await client.get(url)return r.status_codeasync def main():async with httpx.AsyncClient() as client:results = await asyncio.gather(fetch(client, "https://example.com"),fetch(client, "https://python.org"),fetch(client, "https://github.com"),)print(results)asyncio.run(main())
Execution workflow
1async / await Workflow
1 / 4async def defines a coroutine
async def defines a coroutine.
Real-world use
FastAPI, aiohttp, Litestar, and modern Python web stacks are async-first. A single async worker can handle thousands of concurrent connections.
Best practices
- Use async libraries (httpx, asyncpg) — never sync ones inside async fns.
- asyncio.gather for parallel I/O.
- asyncio.run() at the entry point.
Common mistakes
- Calling
requests.getinside async fn blocks the event loop.
Advanced interview questions
Interview Prep
Practice concise answers, then expand each card for the explanation.
1IntermediateQuestionasync vs threads vs processes?+
Answer
asyncio runs many tasks cooperatively on one thread, switching only at await, which suits many concurrent I/O waits with low overhead. Threads are preemptively scheduled and work for blocking I/O libraries, but the GIL prevents parallel Python bytecode. Processes each have their own interpreter and GIL, so they give true CPU parallelism at higher memory and communication cost.Follow-up
How do you call blocking code from an async function?
2IntermediateQuestionWhat is the event loop?+
Answer
The event loop is the scheduler at the core of
asyncio. It runs ready callbacks and tasks, uses the OS selector (such as epoll or kqueue) to watch sockets, and resumes coroutines when the I/O or timer they wait on completes. It runs on a single thread, so anything that blocks the loop stalls every task. Start it with asyncio.run().Follow-up
What happens if a coroutine does CPU-heavy work without awaiting?
3AdvancedQuestionWhen does await yield?+
Answer
await does not always yield. It suspends the current coroutine and returns control to the event loop only if the awaited object actually has to wait, such as an unfinished future or a sleep. If the awaited coroutine completes without suspending, execution continues immediately without a switch. A loop that awaits only already-complete coroutines can still starve other tasks, so use await asyncio.sleep(0) to yield explicitly.Follow-up
What is the difference between
await coro() and asyncio.create_task(coro())?Summary
In summary: Single-thread concurrency for I/O. Never block the event loop.
Ready to mark this lesson complete?Track your journey across the entire course.