iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🌟

Viewing Notes for "import asyncio: Learn Python's AsyncIO"

に公開
2025/09/08

Introduction

import asyncio: Learn Python's is a video that claims to be for beginners on asyncio. Released in 2020, it remains one of the best resources for learning asyncio as of 2025.

https://www.youtube.com/watch?v=Xbl7XjFYsN4&list=PLhNSoGM2ik6SIkVGXWBwerucXjgP1rHmB
Playlist

The author of this video is a member of the Python core team, and the video is referenced as a supplementary resource in the official Python documentation titled A Conceptual Overview of asyncio.

By the way, although it claims to be for beginners, it is only for beginners regarding asyncio itself. You need to be ready to dive into the asyncio library source code, making it a strong-style beginner resource.

Notes by Chapter

1. The Async Ecosystem

This section provides an overview of the async ecosystem.
The discussion up to threads is fairly standard if you have thread knowledge, so it can be skipped.
The historical journey leading to AsyncIO is interesting.

2. The Event Loop

This section discusses the event loop. It starts explaining Lib/asyncio/base_events.py, which raises doubts about its beginner-friendly claim.

  • What is an event loop?
  • How to use AsyncIO event loop from a high-level perspective
  • Reactor vs. Proactor
    • Reactor (UNIX select): A model where the application receives readiness notifications and performs non-blocking read()/write() calls.
    • Proactor (Windows IOCP): A model where the application issues async I/O requests to the OS first ("read/write to this buffer") and receives I/O completion notifications (the OS performs the actual I/O).
  • Verifying how AsyncIO implements the event loop
    • Reading CPython source code (beginner-friendly?)
  • Introduction to uvloop
    • A library that replaces the event loop for better performance

3. Using Coroutines

After watching for over an hour, we finally encounter async/await.
The sample code requires some caution.

  • About async functions and coroutines
  • About await
  • wait_for()
  • Running multiple tasks concurrently with gather
  • Exception handling for TimeoutError and CancelledError
  • Explanation of Future and Task
  • A simple web crawler example

Regarding the async execution method of httpx, it is limited by parameters such as max_connections, so it is assumed that simultaneous access is not unlimited and the server is not overloaded. If possible, it would be better to limit with semaphores, etc.
https://stackoverflow.com/questions/71517645/how-limit-request-per-second-with-httpx-python-3-6?utm_source=chatgpt.com

4 - Coroutines Under The Hood

This section dives deeper into coroutines.
From this point, some samples become difficult to reproduce by pausing the video and running the code.
Not only do you need to read the current library source code, but also past library source code.

  • About Futures
    • How to use them
    • How Futures are implemented
  • What was done in Python 3.4
    • Checking 3.4.1 should match the video
  • About generators
  • Deep dive into Tasks
  • What it looks like in Python 3.8
  • How to debug AsyncIO

5 - Batteries Included

Practical content on asyncio.
It involves database connections, so it's a bit harder to try out easily.
The chat program example for clients and servers can be reproduced by pausing the video.

  • async with
  • async for
  • async generator
  • Example of a chat program for clients and servers
  • Task coordination using queues
  • Lock, Event, Semaphore, Condition

6 - An Example Web Application with Starlette and EdgeDB

Build an async web service using Starlette.
Since this is about a web framework I don't plan to use, I'll skip it for now.

Summary

Watching this while having a vague understanding of asyncio makes you realize your own ignorance and motivates you to start over from scratch.
Since it explains how things actually work by referencing library source code, it takes time but is likely the most understandable content available.

Discussion