DEV Community

qing
qing

Posted on

The Ultimate Python Asyncio Cheat Sheet (2025)

Introduction to Asyncio

Asyncio is a built-in Python library that allows developers to write single-threaded concurrent code using coroutines, multiplexing I/O access over sockets and other resources, and implementing network clients and servers. It's a powerful tool for handling asynchronous programming in Python, making it easier to write efficient and scalable code.

Key Concepts

Before diving into the cheat sheet, let's cover some key concepts in Asyncio:

  • Coroutines: Special types of functions that can suspend and resume their execution at specific points, allowing other coroutines to run in the meantime.
  • Event Loop: The core of every Asyncio program, responsible for managing the execution of coroutines and handling I/O operations.
  • Tasks: Used to run coroutines concurrently, allowing the event loop to switch between them.
  • Futures: Represent the result of a task, providing a way to wait for its completion and retrieve its result.

Syntax Reference

Here's a quick syntax reference for Asyncio:

  • async def: Defines a coroutine function.
  • await: Suspends the execution of a coroutine until the awaited task is complete.
  • asyncio.run(): Runs the top-level entry point for an asyncio program.
  • asyncio.create_task(): Creates a task to run a coroutine concurrently.
  • asyncio.gather(): Runs multiple tasks concurrently and returns their results.

Example: Basic Coroutine

import asyncio

async def hello_world():
    """A simple coroutine that prints 'Hello World'"""
    print("Hello")
    await asyncio.sleep(1)
    print("World")

async def main():
    """The main entry point"""
    await hello_world()

asyncio.run(main())
Enter fullscreen mode Exit fullscreen mode

This example demonstrates a basic coroutine that prints "Hello World" with a 1-second delay between the two words.

Common Patterns

Here are some common patterns in Asyncio:

  • Running multiple tasks concurrently: Use asyncio.gather() to run multiple tasks concurrently and retrieve their results.
  • Handling timeouts: Use asyncio.wait_for() to run a task with a timeout, raising an exception if it doesn't complete within the specified time.
  • Cancelling tasks: Use task.cancel() to cancel a task, and try-except to handle the CancelledError exception.

Example: Concurrent Tasks

import asyncio

async def task1():
    """Task 1: Sleep for 2 seconds"""
    await asyncio.sleep(2)
    return "Task 1 result"

async def task2():
    """Task 2: Sleep for 1 second"""
    await asyncio.sleep(1)
    return "Task 2 result"

async def main():
    """The main entry point"""
    tasks = [task1(), task2()]
    results = await asyncio.gather(*tasks)
    print(results)

asyncio.run(main())
Enter fullscreen mode Exit fullscreen mode

This example demonstrates running multiple tasks concurrently using asyncio.gather() and retrieving their results.

Real-World Examples

Here are some real-world examples of using Asyncio:

  • Web scraping: Use Asyncio to scrape multiple web pages concurrently, improving the overall performance of your web scraping script.
  • API clients: Use Asyncio to make multiple API requests concurrently, reducing the overall latency of your API client.
  • Network servers: Use Asyncio to handle multiple network connections concurrently, improving the scalability of your network server.

Example: Web Scraper

import asyncio
import aiohttp

async def fetch_page(session, url):
    """Fetch a web page"""
    async with session.get(url) as response:
        return await response.text()

async def main():
    """The main entry point"""
    urls = ["http://example.com", "http://python.org"]
    async with aiohttp.ClientSession() as session:
        tasks = [fetch_page(session, url) for url in urls]
        pages = await asyncio.gather(*tasks)
        for page in pages:
            print(page)

asyncio.run(main())
Enter fullscreen mode Exit fullscreen mode

This example demonstrates a simple web scraper that fetches multiple web pages concurrently using Asyncio and aiohttp.

Best Practices

Here are some best practices for using Asyncio:

  • Use asyncio.run() as the main entry point: This ensures that the event loop is properly set up and torn down.
  • Use asyncio.create_task() to run coroutines concurrently: This allows the event loop to switch between coroutines efficiently.
  • Use try-except to handle exceptions: This ensures that your program can recover from unexpected errors.

Conclusion

In this article, we've covered the basics of Asyncio, including syntax, common patterns, and real-world examples. By following the best practices outlined in this article, you can write efficient and scalable asynchronous code using Python's Asyncio library. Whether you're building a web scraper, API client, or network server, Asyncio is a powerful tool to have in your toolkit.

If you want to learn more about Asyncio and stay up-to-date with the latest developments, be sure to follow me for more content. Happy coding!


Found this useful? Follow me on Dev.to for more Python automation tips every week. Drop a comment below — I reply to every one!


🛠️ Useful resource: **Content Creator Ultimate Bundle (Save 33%)* — $29.99. Check it out on Gumroad!*

Top comments (0)