awaitable

In Python, an awaitable is an object that you can use in an await expression. This means you can pause the execution of a coroutine and wait for the awaitable to complete before resuming the coroutine.

There are two main types of awaitable objects: coroutines and objects with an .__await__() method. Coroutines are functions defined with async def, and they can be awaited to yield control back to the event loop, allowing other tasks to run concurrently.

Awaitables are a core part of asynchronous code in Python. They enable you to write non-blocking code by allowing other tasks to run while waiting for an I/O operation or any other time-consuming task to complete. This results in more efficient and responsive programs, especially when dealing with I/O-bound operations, such as network requests or file handling.

Example

Here’s a quick example of using an awaitable in an asynchronous function:

Python
>>> import asyncio

>>> async def say_hello():
...     print("Hello")
...     await asyncio.sleep(1)
...     print("World")
...

>>> # Usage
>>> asyncio.run(say_hello())
Hello
World

In this example, asyncio.sleep(1) returns an awaitable that pauses the say_hello() coroutine for one second. During this pause, the async event loop can run other tasks, making your program more efficient.

Tutorial

Getting Started With Async Features in Python

This step-by-step tutorial gives you the tools you need to start making asynchronous programming techniques a part of your repertoire. You'll learn how to use Python async features to take advantage of IO processes and free up your CPU.

intermediate python

For additional information on related topics, take a look at the following resources:


By Leodanis Pozo Ramos • Updated Jan. 24, 2025 • Reviewed by Dan Bader