Adventures in Machine Learning

Boost Your Python I/O Performance with Async IO

Introduction to Async IO

Are you tired of writing code that performs poorly with slow I/O? Are you tired of waiting for your program to finish its I/O operations before moving on to the next task?

Luckily, Python’s Async IO provides a solution to these issues by allowing developers to write asynchronous, non-blocking code that executes faster than traditional blocking code. In this article, we will discuss what Async IO is, where it fits in the world of concurrency, and how it works.

What is Async IO? Async IO is a concurrent programming design that utilizes coroutines to perform I/O operations asynchronously.

It coordinates non-blocking I/O operations that carry out immediately without waiting for the results to be returned. Async IO is built using cooperative multitasking, a design that allows each coroutine to temporarily pause its execution and give a chance to another coroutine to execute.

This design enables efficient utilization of system resources since it can handle large numbers of I/O-bound tasks in a single thread. When a coroutine pauses its execution, it simply switches to another coroutine.

This process allows for immediate response to other events that may want to be handled simultaneously. In short, Async IO is a technique of writing non-blocking I/O functions that provide better response time for I/O-bound tasks.

Where does Async IO fit in? Concurrency refers to the ability to deal with multiple tasks that may execute side by side.

There are various ways to implement concurrency in Python, including multiprocessing, threading, and parallelism. However, these methods come with trade-offs, such as shared memory usage, locking mechanisms, and a high risk of deadlocks and race conditions.

Alternatively, Async IO offers a simpler solution that uses a single thread and cooperative multitasking to execute multiple I/O-bound tasks.

Parallelism encompasses dividing a program into smaller subtasks that can be processed simultaneously on multiple processors.

Asynchronous programming is a technique that helps maximize the potential of parallelism by making sure each process always has something to do, avoiding idle time between tasks. Async IO is uniquely designed to take advantage of parallelism.

It helps to execute I/O-bound tasks without the overhead and delays of the traditional techniques.

Async IO Explained

Prior to the introduction of Async IO, the standard way of managing concurrency was to use threads. However, several issues come with threads, such as switching of control among threads under different conditions.

This leads to uncertainty, making it more difficult to manage the overall concurrency. Also, threading comes with its limitation and complexity.

To overcome these problems, Async IO utilizes coroutines and cooperative multitasking to switch between different I/O tasks efficiently. A coroutine is a piece of code that is executed, paused, and resumed at a specific point in the function.

In Async IO, a coroutine can be initiated and paused according to signals given from the event loop. The event loop is an essential part of Async IO and controls event-driven programs by managing input/output (I/O).

When a coroutine awaits I/O, like reading a web page, the event loop dispatches another coroutine so that it can continue executing. This process of switching between coroutines allows for better responsiveness and improved memory usage.

In simple terms, Async IO works by using an event loop that manages the execution of coroutines, ensuring that they execute efficiently.

Setting Up Your Environment

Requirements for Async IO

Python 3.7 supports Async IO, so ensure that you have the latest version of Python installed. You’ll also need to install some libraries that can handle Async IO operations; aiohttp is a must-have for web scraping, while aiofiles assists with I/O operations.

Environment Setup

Creating a virtual environment ensures you can keep the dependencies of your Async IO project separate from the other applications on your computer. Virtual environments are used to ensure your project runs successfully with all the correct dependencies.

To create a virtual environment, use the following command in your terminal.


python -m venv async_env


This creates a directory called async_env, which contains the virtual environment. To activate the environment, enter the following command:


source async_env/bin/activate



In conclusion, Async IO is a powerful programming design that allows for better responsiveness when handling I/O-bound tasks. It makes it possible to develop efficient programs without utilizing complex threading and multiprocessing techniques.

In conjunction with future releases of Python, the importance of Async IO in the world of concurrency will only continue to grow. Hopefully, this article has given you a practical understanding of Async IO, and the next time you’re writing code that has significant I/O operations, you’ll be able to reduce the waiting time and improve responsiveness.

3) The 10,000-Foot View of Async IO

Overview of Concurrency and Parallelism

Concurrency refers to a program’s ability to execute multiple tasks at the same time. It enables a program to handle several operations simultaneously, which would otherwise be impossible without the use of concurrency.

Concurrency can be implemented in two ways: multi-threading and multi-processing.

Multi-threading involves the use of multiple threads within the same process running concurrently, each with its own set of instructions.

Multi-processing, on the other hand, involves using multiple processes, each with its own memory space and set of instructions to carry out. Parallelism, on the other hand, refers to the ability of a program to split tasks into smaller subtasks, which can be processed simultaneously on multiple processors/cores to reduce processing time.

The implementation of parallelism requires efficient utilization of hardware resources, making it considerably more complex than concurrency. to Async IO

Async IO is a programming design that enables asynchronous functions in Python.

It allows the development of non-blocking I/O code that executes faster and more efficiently than traditional blocking code. Async IO achieves this by allowing a program to function concurrently through the use of coroutines.

Async IO works by utilizing a single thread and cooperative multitasking to execute multiple I/O-bound asyncio tasks concurrently. Async IO can handle vast numbers of I/O-bound tasks in a single thread, which makes it quite efficient compared to the traditional concurrency models such as multi-threading and multi-processing.

Async IO is not designed for CPU-bound tasks that consume significant amounts of computations or processing. Instead, it is designed to enhance the performance of I/O-bound tasks by avoiding I/O waits and idle CPU time between tasks.


Async IO Explained

Difficulty of Async IO

While Async IO offers many benefits, it can be a challenging concept for developers new to Python or asynchronous programming. Traditional concurrency models such as multi-threading or multi-processing make it easier to parallelize such programs.

In contrast, Async IO excels in handling tasks that are I/O-bound, and it can run concurrently without the usual complexities associated with multi-threading. Async IO is not without issues, and it may require an understanding of a few concepts such as coroutines, event loops, and cooperative multitasking.

However, once mastered, Async IO can reduce code complexity, allow for better scaling, and reduce the number of resources a program requires.

Async IO and Coroutines

Coroutines are an essential part of the Async IO model of concurrent programming. A coroutine is a method that can pause and resume its execution in contrast to a standard Python function that runs to completion.

Coroutines are useful in Async IO because they provide a lightweight mechanism for switching between concurrent tasks compared to the complex multi-threading mechanisms required in traditional parallelization techniques. In Python, coroutines are built using generator functions.

A generator function is a special kind of Python function that returns an iterator that can be iterated over. In Async IO, a generator function is transformed into a coroutine using the keyword “async,” which makes the function an asynchronous Python function or coroutine.

When a coroutine is called and executed, it runs like any other Python function, executing until it either returns or reaches the await keyword. When the await keyword is encountered, the coroutine suspends its execution and returns control flow to the event loop.

The event loop then continues running and allows other coroutines to execute until the awaited I/O operation completes.

Once the awaited I/O operation completes, the event loop signals the suspended coroutine to continue its execution until it also hits the await keyword again or the coroutine completes execution.

This mechanism of coordinating coroutines using the event loop is called cooperative multitasking, which is the key

to Async IO’s efficient operation.


Async IO offers a powerful and efficient way to write non-blocking, I/O-bound tasks in Python. By using coroutines, the cooperative multitasking model, and the event loop, Python programs can execute multiple concurrent tasks efficiently, saving system resources and reducing the waiting time.

While it may have a learning curve, mastering Async IO brings significant benefits that outshine the complexities associated with it. Programming requires techniques that optimize response time, and Async IO provides developers with an optimal solution.

5) The asyncio Package and async / await

Purpose of async/await and asyncio

Asyncio is a Python library for writing concurrent code that often requires I/O operations. Asyncio functions achieve this by using coroutines, which enable the functions to execute cooperatively using a single thread.

The asyncio library provides high-level APIs for writing concurrent I/O code.

Async/await is a Python syntax that provides concise syntax for writing coroutines.

The syntax was introduced in Python 3.5, and it simplifies asynchronous programming by allowing the developer to write asynchronous code in a synchronous style. The async/await syntax leverages the asyncio library to write coroutine functions that can be executed concurrently and non-blockingly.

Rules of Async IO

Asynchronous programming in Python can be challenging to work with, given the complexity of coroutines and other associated concepts. These are the basic rules for working with coroutines in Async IO:


Coroutine functions must be defined using ‘async def’ instead of ‘def’. By using ‘async def’, coroutines also automatically become awaitables.

2. When awaiting another coroutine function, call it with the ‘await’ keyword.

The ‘await’ keyword will prevent the coroutine from blocking while the awaited function is running, allowing other coroutines to run concurrently. 3.

Coroutines should return something using ‘return’ keyword when they complete. If it just needs to exit, then use ‘return None’.

4. Yielding values is allowed, but it is essential to use ‘yield from’ or ‘await’ expressions in the appropriate context.

6) Async IO Design Patterns

Chaining Coroutines

It’s possible to chain coroutines together to process a more elaborate task. When chaining coroutines, the output of one coroutine is passed as input to the next.

This makes it possible to perform a sequence of I/O-bound tasks without blocking the main thread. Here’s an example:


async def coroutine1():

print(“starting coroutine1”)

await asyncio.sleep(1)

print(“finishing coroutine1”)

return “Coroutine 1 result”

async def coroutine2(result):

print(“starting coroutine2”, result)

await asyncio.sleep(2)

print(“finishing coroutine2”)

return “Coroutine 2 result”

async def coroutine3(result):

print(“starting coroutine3”, result)

await asyncio.sleep(3)

print(“ending coroutine3”)

async def main():

res1 = await coroutine1()

res2 = await coroutine2(res1)

await coroutine3(res2)

loop = asyncio.get_event_loop()



In this example, coroutine2 waits for the result from coroutine1 before running, and coroutine3 waits for the result from coroutine2.

Therefore, the coroutines are executed one after the other.

Using a Queue

Queues can be used to manage concurrent operations effectively in Async IO. A queue is a thread-safe, asynchronous data structure that’s used to store data.

Using a queue helps developers manage and organize tasks more efficiently. Here is an example:


import asyncio

from asyncio import Queue

async def download_file(q, file_url):

print(f”Downloading file: {file_url}”)

await asyncio.sleep(3)

await q.put(file_url)

print(f”Download complete: {file_url}”)

async def process_file(q):

while True:

file_url = await q.get()

print(f”Processing file: {file_url}”)

await asyncio.sleep(5)

print(f”Processing complete: {file_url}”)


async def main():

file_urls = [“”, “”, “”]

q = Queue()

for file_url in file_urls:

await download_file(q, file_url)

tasks = []

for i in range(5):

task = asyncio.create_task(process_file(q))


await q.join()

for task in tasks:


loop = asyncio.get_event_loop()



In this example, the `download_file` function downloads a file and then puts the URL of the file into the queue. The `process_file` function then processes each file URL in the queue.

Additionally, we have created five worker tasks via the `create_task` function to consume the URLs from the queue and process the downloaded file. Once all the tasks have completed, the `await q.join()` method is used to block until all tasks have completed, and then we cancel the worker tasks using `task.cancel()`.


In conclusion, Async IO and the asyncio library offer an efficient and straightforward way to write concurrent and non-blocking code in Python. Combining the async/await syntax with the asyncio library achieves this feat by enabling developers to write applications that handle I/O operations concurrently.

In this article, we’ve explored some of the basic concepts of Async IO, including coroutines, the asyncio module, the ‘async/await’ syntax, and common Async IO design patterns, such as chaining coroutines and using queues, that make the development of non-blocking code effortless and efficient. 7) Async IO’s Roots in Generators

Generators in Python

Generators are functions that use ‘yield’ to produce a sequence of values. They maintain their state between calls and can be paused and resumed when they’re called again.

Here is an example of a generator that generates even numbers:


def even_numbers_generator():

i = 0

while True:

yield i

i += 2


When you call the even_numbers_generator, it produces an iterable sequence of even numbers.

Coroutine Functions

In Python, coroutines are built on top of generators, using the ‘yield’ keyword to pause and resume execution. Python 3.3 introduced ‘yield from,’ which made it possible to delegate to another generator function or coroutine.

In Python 3.5, ‘async/await’ syntax was added to make it easier to write coroutines, creating a more straightforward way to write asynchronous operations.

Here is an example of a coroutine that uses ‘async/await’:


async def coroutine_async():

print(“Starting coroutine_async”)

await asyncio.sleep(1)

print(“Finishing coroutine_async”)

return “Coroutine async result”


The ‘async def’ syntax indicates that this is a coroutine, and the ‘await’ keyword indicates an operation that returns a ‘Future’ object.

8) Other Features: async for and Async Generators + Comprehensionsto async for

The ‘async for’ loop is a new feature introduced in Python 3.5 that allows you to iterate over an asynchronous iterable. The loop will execute concurrently, allowing you to perform asynchronous operations as you iterate over your collection.

The ‘async for’ loop can also be used with ‘async with’ statements to manage a resource that requires cleanup. Here’s an example of using the ‘async for’ loop with a coroutine:


async def read_data():

async with“data.txt”) as f:

async for line in f:



In this example, the ‘’ method opens a file asynchronously using the ‘async with’ statement.

‘async for’ then iterates over the lines in the file asynchronously and prints them to the Async Generators + Comprehensions

An asynchronous generator is a variation of a generator that can be iterated asynchronously using the ‘async for’ loop. Async generators are useful when you need to generate a sequence of values asynchronously.

Here is an example:


async def async_generator():

for i in range(3):

Popular Posts