On this page

Asyncio & Coroutines

Enrico Hochkoeppler Profile

By Enrico Hochkoeppler

Web Developer & TutoriaLibre Founder

Why asyncio exists​

Many modern programs must handle large numbers of concurrent operations: network requests, database calls, message queues, file I/O, and other tasks that spend most of their time waiting. Traditional threads can handle this, but they introduce overhead, complexity, and synchronization challenges.

Asyncio provides a different model:

Asyncio is ideal for I/O‑bound systems such as web servers, API clients, crawlers, and real‑time applications.

What a coroutine is​

A coroutine is a function defined with async def.

It does not run immediately. Instead, it returns a coroutine object, which the event loop schedules and executes.

Example:


async def fetch_data():

    return "result"

Calling fetch_data() does not execute the function.

It creates a coroutine that must be awaited:


result = await fetch_data()

Coroutines pause and resume at await points, allowing other coroutines to run.

The event loop​

The event loop is the core of asyncio. It schedules and runs coroutines, switching between them whenever they await an I/O operation.

Running a coroutine:


import asyncio



async def main():

    print("Hello")



asyncio.run(main())

asyncio.run() creates an event loop, runs the coroutine, and closes the loop automatically.

Awaitables and the await keyword​

A coroutine can only pause at an awaitable.

Awaitables include:

Example:


await asyncio.sleep(1)

sleep() is non‑blocking: it suspends the coroutine and lets the event loop run other tasks.

Running tasks concurrently​

To run coroutines concurrently, convert them into tasks.


import asyncio



async def worker(n):

    await asyncio.sleep(1)

    return n



async def main():

    tasks = [asyncio.create_task(worker(i)) for i in range(5)]

    results = await asyncio.gather(*tasks)

    print(results)



asyncio.run(main())

asyncio.gather() waits for all tasks and returns their results.

This pattern is essential for high‑throughput asynchronous systems.

Async context managers​

Asyncio supports asynchronous setup and cleanup using async context managers.


class AsyncResource:

    async def __aenter__(self):

        print("Opening resource")

        return self



    async def __aexit__(self, exc_type, exc, tb):

        print("Closing resource")

Usage:


async with AsyncResource() as r:

    ...

Async context managers are used for:

Async iterators​

Asyncio also supports asynchronous iteration using async for.


class AsyncCounter:

    def __init__(self, limit):

        self.limit = limit

        self.current = 0



    def __aiter__(self):

        return self



    async def __anext__(self):

        if self.current >= self.limit:

            raise StopAsyncIteration

        await asyncio.sleep(0.1)

        value = self.current

        self.current += 1

        return value

Usage:


async for n in AsyncCounter(3):

    print(n)

Async iterators are useful when retrieving data from asynchronous sources.

Synchronization primitives​

Asyncio provides non‑blocking versions of common synchronization tools:

Example:


lock = asyncio.Lock()



async def safe_update():

    async with lock:

        ...

These primitives ensure safe coordination between coroutines without blocking the event loop.

Designing asynchronous workflows​

Asyncio is appropriate when:

Asyncio is not appropriate for CPU‑bound tasks; use multiprocessing instead.

Principles for reliable asyncio code​

Asyncio provides a powerful, scalable concurrency model for modern Python applications, enabling high‑performance asynchronous workflows with clear, maintainable structure.

Found this helpful? Share the knowledge...

... or support this project to keep it alive!

Buy Me a Coffee

Previous

Multiprocessing

Next

Performance Optimization