Code Reference

Async

Python · Reference cheat sheet

Async

Python · Reference cheat sheet


📋 Overview

async / await run concurrent I/O with an event loop (asyncio). Prefer async for many waiting network/file ops; use threads/processes for CPU-bound work.

🔧 Core concepts

PieceRole
async defCoroutine function
awaitSuspend until awaitable completes
asyncio.run(main())Entry point
asyncio.gatherRun many coroutines
asyncio.create_taskSchedule concurrently
asyncio.sleepNon-blocking delay

💡 Examples

import asyncio

async def fetch(n: int) -> int:
    await asyncio.sleep(0.05)
    return n * 2

async def main() -> None:
    results = await asyncio.gather(fetch(1), fetch(2), fetch(3))
    print(results)

asyncio.run(main())
import asyncio

async def worker(name: str) -> None:
    print(name, "start")
    await asyncio.sleep(0.1)
    print(name, "done")

async def main() -> None:
    t1 = asyncio.create_task(worker("a"))
    t2 = asyncio.create_task(worker("b"))
    await t1
    await t2

asyncio.run(main())

⚠️ Pitfalls

  • Never call blocking I/O inside coroutines without an executor.
  • Forgetting await returns a coroutine object (often a warning).
  • asyncio.run cannot nest inside a running loop.
  • Mixing sync Django views with async requires care (ASGI).

On this page