跳转至

2. Asyncio 是什么

2. What is Asyncio

广义上,asyncio 是指在 Python 中使用协程实现异步编程的能力。

具体来说,它指的是两个要素:

  1. Python 3.4 中向 Python 标准库添加了 “asyncio” 模块。
  2. Python 3.5 中向 Python 语言添加了 async/await 表达式。

该模块的增加以及对语言的更改共同促进了支持基于协程的并发、非阻塞 I/O 和异步编程的 Python 程序的开发。

Python 3.4 引入了 asyncio 库,Python 3.5 生成了 async 和await 关键字以方便地使用它。 这些新增功能允许所谓的异步编程。

— PAGE VII, USING ASYNCIO IN PYTHON, 2020.

让我们从语言的变化开始,仔细看看 asyncio 的这两个方面。

Broadly, asyncio refers to the ability to implement asynchronous programming in Python using coroutines.

Specifically, it refers to two elements:

  1. The addition of the “asyncio” module to the Python standard library in Python 3.4.
  2. The addition of async/await expressions to the Python language in Python 3.5.

Together, the module and changes to the language facilitate the development of Python programs that support coroutine-based concurrency, non-blocking I/O, and asynchronous programming.

Python 3.4 introduced the asyncio library, and Python 3.5 produced the async and await keywords to use it palatably. These new additions allow so-called asynchronous programming.

— PAGE VII, USING ASYNCIO IN PYTHON, 2020.

Let’s take a closer look at these two aspects of asyncio, starting with the changes to the language.

2.1 对 Python 进行更改以添加对协程的支持

2.1 Changes to Python to add Support for Coroutines

Python 语言已进行更改,以通过添加表达式和类型来适应 asyncio。

更具体地说,它被更改为支持协程作为一流概念。 反过来,协程是异步程序中使用的并发单位。

协程是一个可以暂停和恢复的函数。

协程(coroutine): 协程是更通用的子例程形式。 子程序在某一点进入并在另一点退出。 协程可以在许多不同的点进入、退出和恢复。

PYTHON GLOSSARY

协程可以通过 “async def” 表达式来定义。 它可以接受参数并返回一个值,就像函数一样。

例如:

# 定义一个协程
async def custom_coro():
    # ...

调用协程函数会创建一个协程对象,这是一个新类。 它不执行协程函数。

...
# 创建一个协程对象
coro = custom_coro()

一个协程可以通过 await 表达式执行另一个协程。

这会挂起调用者并安排目标执行。

...
# 暂停并安排目标
await custom_coro()

异步迭代器是产生可等待对象的迭代器。

异步迭代器(asynchronous iterator): 实现 __aiter__() 和 __anext__() 方法的对象。 __anext__ 必须返回一个可等待的对象。 async for 解析异步迭代器的 __anext__() 方法返回的等待对象,直到引发 StopAsyncIteration 异常。

PYTHON GLOSSARY

可以使用 “async for” 表达式遍历异步迭代器。

...
# 遍历异步迭代器
async for item in async_iterator:
    print(item)

这不会并行执行 for 循环。

相反,执行 for 循环的调用协程将挂起并在内部等待迭代器生成的每个可等待项

异步上下文管理器是可以等待进入和退出方法的上下文管理器。

异步上下文管理器是能够在其进入和退出方法中暂停执行的上下文管理器。

ASYNCHRONOUS CONTEXT MANAGERS AND “ASYNC WITH”

“async with” 表达式用于创建和使用异步上下文管理器。

在进入上下文管理器块之前,调用协程将挂起并等待上下文管理器,在离开上下文管理器块时也是如此。

这些是 Python 语言为支持协程而进行的主要更改的总和。

接下来,让我们看看 asyncio 模块。

The Python language was changed to accommodate asyncio with the addition of expressions and types.

More specifically, it was changed to support coroutines as first-class concepts. In turn, coroutines are the unit of concurrency used in asyncio programs.

A coroutine is a function that can be suspended and resumed.

coroutine: Coroutines are a more generalized form of subroutines. Subroutines are entered at one point and exited at another point. Coroutines can be entered, exited, and resumed at many different points.

PYTHON GLOSSARY

A coroutine can be defined via the “async def” expression. It can take arguments and return a value, just like a function.

For example:

# define a coroutine
async def custom_coro():
    # ...

Calling a coroutine function will create a coroutine object, this is a new class. It does not execute the coroutine function.

...
# create a coroutine object
coro = custom_coro()

A coroutine can execute another coroutine via the await expression.

This suspends the caller and schedules the target for execution.

...
# suspend and schedule the target
await custom_coro()

An asynchronous iterator is an iterator that yields awaitables.

asynchronous iterator: An object that implements the __aiter__() and __anext__() methods. __anext__ must return an awaitable object. async for resolves the awaitables returned by an asynchronous iterator’s __anext__() method until it raises a StopAsyncIteration exception.

PYTHON GLOSSARY

An asynchronous iterator can be traversed using the “async for” expression.

...
# traverse an asynchronous iterator
async for item in async_iterator:
    print(item)

This does not execute the for-loop in parallel.

Instead, the calling coroutine that executes the for loop will suspend and internally await each awaitable yielded from the iterator.

An asynchronous context manager is a context manager that can await the enter and exit methods.

An asynchronous context manager is a context manager that is able to suspend execution in its enter and exit methods.

ASYNCHRONOUS CONTEXT MANAGERS AND “ASYNC WITH”

The “async with” expression is for creating and using asynchronous context managers.

The calling coroutine will suspend and await the context manager before entering the block for the context manager, and similarly when leaving the context manager block.

These are the sum of the major changes to Python language to support coroutines.

Next, let’s look at the asyncio module.

2.2 asyncio 模块

2.2 The asyncio Module

“asyncio”模块提供了使用异步编程范例开发基于协程的程序的函数和对象。

具体来说,它支持带有子进程(用于执行命令)和流(用于 TCP 套接字编程)的非阻塞 I/O。

asyncio 是一个使用 async/await 语法编写并发代码的库。

ASYNCIO — ASYNCHRONOUS I/O

asyncio 模块的核心是事件循环

这是运行基于协程的程序并实现协程之间协作多任务的机制。

事件循环是每个异步应用程序的核心。 事件循环运行异步任务和回调、执行网络 IO 操作并运行子进程。

ASYNCIO EVENT LOOP

该模块提供高级和低级 API。

高级 API 适合我们 Python 应用程序开发人员。 在大多数情况下,低级 API 是为框架开发人员提供的,而不是我们。

大多数用例都可以使用高级 API 来满足,该 API 提供了用于处理协程、流、同步基元、子进程和队列以在协程之间共享数据的实用程序。

较低级别的 API 为高级 API 提供基础,包括事件循环、传输协议、策略等的内部结构。

… 有供库和框架开发人员使用的低级 API

ASYNCIO — ASYNCHRONOUS I/O

现在我们大致了解了 asyncio 是什么,并且它用于异步编程。

接下来,让我们探讨一下何时应该考虑在 Python 程序中使用 asyncio。

对 asyncio 模块 API 感到困惑吗?

下载我的免费 PDF 备忘单

The “asyncio” module provides functions and objects for developing coroutine-based programs using the asynchronous programming paradigm.

Specifically, it supports non-blocking I/O with subprocesses (for executing commands) and with streams (for TCP socket programming).

asyncio is a library to write concurrent code using the async/await syntax.

ASYNCIO — ASYNCHRONOUS I/O

Central to the asyncio module is the event loop.

This is the mechanism that runs a coroutine-based program and implements cooperative multitasking between coroutines.

The event loop is the core of every asyncio application. Event loops run asynchronous tasks and callbacks, perform network IO operations, and run subprocesses.

ASYNCIO EVENT LOOP

The module provides both a high-level and low-level API.

The high-level API is for us Python application developers. The low-level API is for framework developers, not us, in most cases.

Most use cases are satisfied using the high-level API that provides utilities for working with coroutines, streams, synchronization primitives, subprocesses, and queues for sharing data between coroutines.

The lower-level API provides the foundation for the high-level API and includes the internals of the event loop, transport protocols, policies, and more.

… there are low-level APIs for library and framework developers

ASYNCIO — ASYNCHRONOUS I/O

Now that we know what asyncio is, broadly, and that it is for Asynchronous programming.

Next, let’s explore when we should consider using asyncio in our Python programs.

Confused by the asyncio module API?

Download my FREE PDF cheat sheet


最后更新: 2024年9月4日
创建日期: 2024年9月4日