Asynchronous programming in Python

Abhilash
3 min readMar 30, 2023

--

Asynchronous programming has become increasingly popular in recent years due to its ability to handle multiple tasks efficiently, especially in the world of web development. In this article, we will explore asynchronous programming in Python, providing a beginner’s guide and examples to help you understand how it works and how it can be used in your projects.

What is Asynchronous Programming?

Before we delve into the specifics of asynchronous programming in Python, let’s first define what it is. Asynchronous programming is a programming paradigm that allows multiple tasks to be executed concurrently. Instead of executing one task at a time in a sequential manner, asynchronous programming enables different tasks to be executed simultaneously, improving the overall performance and efficiency of the program.

Asynchronous programming is particularly useful in situations where there is a high level of I/O operations, such as web servers and applications that need to handle many concurrent requests. In these scenarios, traditional synchronous programming can result in blocking I/O operations, causing a bottleneck in the program’s performance.

How Asynchronous Programming Works in Python

Python has a built-in library for asynchronous programming called asyncio. asyncio provides a set of high-level APIs for asynchronous programming that makes it easy to implement asynchronous functionality in your Python programs.

At the core of asyncio is the concept of coroutines. A coroutine is a special type of function that can be paused and resumed, allowing other tasks to be executed in the meantime. Coroutines are defined using the async keyword and can be used with the await keyword to pause the coroutine until a particular task has been completed.

Here’s an example of a simple coroutine in Python:

import asyncio

async def hello():
print(“Hello”)
await asyncio.sleep(1)
print(“World”)

asyncio.run(hello())

In this example, we define a coroutine called hello that prints “Hello”, waits for one second using asyncio.sleep, and then prints “World”. We then use asyncio.run to run the coroutine.

The key thing to note here is the use of the await keyword to pause the coroutine while asyncio.sleep is executed. This allows other tasks to be executed in the meantime, improving the overall performance of the program.

Asynchronous Programming Example: Web Scraper

Let’s take a look at an example of how asynchronous programming can be used to improve the performance of a web scraper. In this example, we’ll use Python’s asyncio library and the aiohttp library to asynchronously fetch the content of multiple web pages.

import asyncio
import aiohttp

async def fetch(session, url):
async with session.get(url) as response:
return await response.text()

async def main():
async with aiohttp.ClientSession() as session:
tasks = []
for url in [“http://www.example.com", “http://www.example.org", “http://www.example.net"]:
task = asyncio.ensure_future(fetch(session, url))
tasks.append(task)
pages = await asyncio.gather(*tasks)
print(pages)

loop = asyncio.get_event_loop()
loop.run_until_complete(main())

In this example, we define a coroutine called fetch that takes a session object and a URL as input and fetches the content of the web page using the aiohttp library. We then define a coroutine called main that creates a client session using aiohttp and uses asyncio.gather to execute multiple fetch coroutines concurrently.

The result of the fetch coroutines is collected using asyncio.gather and printed to the console. Finally, we use the asyncio event loop to run the main coroutine.

By using asynchronous programming, we can fetch the content of multiple web pages concurrently, improving the overall performance of the web scraper.

--

--