Python Tutorial 0/52 lessons ~6 min read Lesson 39

    async / await

    async/await enables single-thread concurrency for I/O-bound code — perfect for web servers, scrapers, and pipelines doing many network calls.

    Course progress0%
    Focus
    9 guided sections
    Practice signal
    Examples included
    Career prep
    Foundation builder

    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 def defines a coroutine.
    • await yields 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, httpx
    async def fetch(client, url):
    r = await client.get(url)
    return r.status_code
    async 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 / 4

    Step 1

    async def defines a coroutine.

    Apply this step while implementing async / await in real code.

    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.get inside async fn blocks the event loop.

    Hands-on exercise

    Interview preparation — practice these questions:

    • async vs threads vs processes?
    • What is the event loop?
    • When does await yield?

    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.