Python · Stage 10 – Beyond the Basics · Lesson 49 of 50

Async I/O

Understand asynchronous waiting through one simple example and analogy.

import asyncio

async def wait_briefly():
    await asyncio.sleep(1)
    print("Finished")
Line-by-line explanation
  1. Asyncio provides asynchronous tools.
  2. async def defines a coroutine.
  3. await pauses this task without blocking all async work.
  4. After one second, Finished is displayed.
asyncio.run(wait_briefly())
Line-by-line explanation
  1. The coroutine is created by calling it.
  2. asyncio.run manages the asynchronous event loop.
  3. The program runs until the coroutine finishes.