Python asyncio package

This post discusses python asyncio module.

async IO

Asynchronous IO is a language-agnostic paradigm that has implementations across a host of programming languages. asyncio package in python provides API to run coroutines.

async IO gives you a feeling of concurrency despite using a single thread in a single process. Asynchronous routines are able to “pause” while waiting on their ultimate result and let other routines run in the meantime.

For example:

import asyncio

async def count():
    await asyncio.sleep(1)

async def main():
    await asyncio.gather(count(), count(), count())

if __name__ == "__main__":
    import time
    s = time.perf_counter()

This simple code block above, when run, will output:

  • async def introduces either a native coroutine or an asynchronous generator. To call a coroutine function, you must await it to get its results.

  • await passes function control back to event loop. If Python encounters an await f() expression in the scope of g(), this is how await tells the event loop, Suspend execution of g() until whatever I’m waiting on. It is a syntax error to use await outside async def function.

  • It is less common to use yield in an async def block. This creates an asynchronous generator.

  • When we use await f(), f() has to be an object that is awaitable. For now, just know that an awaitable object is another coroutine.


A coroutine is a specialized version of a Python generator function.


  1. asyncio official documentation