Fundamentals 6 min read

Master Python asyncio: From Coroutines to Event Loops Explained

Learn how Python's asyncio library enables asynchronous I/O by defining coroutines with async/await, managing event loops, handling futures, using gather and wait, and properly closing loops, with clear code examples illustrating each concept and practical tips for concurrent programming.

MaGe Linux Operations
MaGe Linux Operations
MaGe Linux Operations
Master Python asyncio: From Coroutines to Event Loops Explained

Asyncio Overview

Python's asyncio provides asynchronous I/O similar to C++ Boost.Asio. It allows you to start an I/O operation without waiting for its completion, enabling other work to proceed and receiving a notification when the operation finishes.

Asyncio is one form of concurrency; Python can also achieve concurrency via threading and multiprocessing. However, asyncio does not provide true parallelism, and due to the Global Interpreter Lock (GIL), Python threads also lack true parallel execution.

Tasks that can be scheduled by asyncio are called coroutines. A coroutine can yield execution to others using await or yield from.

Defining Coroutines

Coroutines are defined with the async def syntax. For example:

async def do_some_work():
    await asyncio.sleep(2)
    return "result"

You can verify a function is a coroutine function with asyncio.iscoroutinefunction(do_some_work).

Running Coroutines

Calling a coroutine function returns a coroutine object; it does not start execution. You can check with asyncio.iscoroutine(coro). To run it, obtain the event loop and use loop.run_until_complete(coro), which blocks until the coroutine finishes.

Alternatively, you can schedule the coroutine with loop.create_task(coro) and then start the loop with loop.run_forever(), stopping it later with loop.stop().

Callbacks and Futures

When a coroutine represents an I/O read, you can attach a callback to its future to be notified when data is ready, enabling further processing.

Multiple Coroutines

In real projects, several coroutines often run concurrently in the same loop. Use asyncio.gather to combine them into a single future:

results = await asyncio.gather(coro1(), coro2(), coro3())

You can also collect coroutine objects in a list and pass the list to gather.

run_until_complete vs. run_forever

run_until_complete

runs the loop until the given future completes, then returns. run_forever keeps the loop running indefinitely until loop.stop() is called, which must be invoked from within a coroutine.

Closing the Loop

The event loop can be reused as long as it remains open. Closing the loop with loop.close() releases resources and prevents accidental reuse.

gather vs. wait

Both asyncio.gather and asyncio.wait aggregate multiple awaitables, but they differ in return values and error handling; refer to the official documentation for details.

Timer Emulation

While C++ Boost.Asio offers a native timer object, Python's asyncio does not have a dedicated timer API. You can simulate a timer using await asyncio.sleep(seconds).

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

coroutineevent loopasynchronous-ioasyncio
MaGe Linux Operations
Written by

MaGe Linux Operations

Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.