Skip to content

asyncio

(https://docs.python.org/3/library/asyncio.html) is a library to write concurrent code using the async/await syntax.

Basic example:

"""Asyncio example."""

import asyncio
from typing import Dict


async def function(number: int) -> str:
    """Sleep 1 second and return the passed number as a string."""
    await asyncio.sleep(1)
    return str(number)


async def main() -> None:
    """Call function 100 times and save the outputs in a dictionary."""
    args = list(range(100))
    results = await asyncio.gather(*)

    result: Dict = dict(zip(args, results))
    print(result)


asyncio.run(main())

This code runs in approximately one second, instead of the expected 100 seconds of its non concurrent version.

Tips

Limit concurrency

Use (https://docs.python.org/3/library/asyncio-sync.html#semaphores).

sem = asyncio.Semaphore(10)

async with sem:
    # work with shared resource

Note that this method is not thread-safe.