生成器功能和异步生成器功能的用途有什么区别 [英] What are the differences between the purposes of generator functions and asynchronous generator functions

查看:152
本文介绍了生成器功能和异步生成器功能的用途有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Python中,异步生成器函数是协程,生成器函数也是协程.

In Python, asynchronous generator functions are coroutines, and generator functions are also coroutines.

生成器功能和异步生成器功能的用途有什么区别?

What are the differences between the purposes of generator functions and asynchronous generator functions?

谢谢.

推荐答案

PEP 525-异步生成器 PEP 255非常相似- -简单的生成器,其中引入了生成器.主要目的是使事情更容易实现,仅在不同的域(异步域)中才能实现.从PEP 525:

The purpose of PEP 525 -- Asynchronous Generators is pretty much similar to PEP 255 -- Simple Generators which introduced generators. It is mainly intented to make things easier to implement, only in a different domain (asynchronous one). From PEP 525:

从本质上讲,适用于异步执行情况的PEP 255的目标和基本原理也适用于该建议.

Essentially, the goals and rationale for PEP 255, applied to the asynchronous execution case, hold true for this proposal as well.

简而言之,它使编写支持异步迭代协议的对象简单.正如 generators iterator协议所做的那样.

In short, it makes writing objects that support the asynchronous iteration protocol easy. As generators did for the iterator protocol.

您不必创建实现__aiter____anext__的对象,而是创建一个异步生成器,该生成器似乎可以通过魔术来实现.这反映了生成器对迭代器协议所做的工作;无需为对象实现__iter____next__,您只需创建一个生成器即可.

Instead of having to define objects that implement __aiter__ and __anext__ you create an asynchronous generator that does this seemingly by magic. This mirrors what generators did for the iterator protocol; instead of implementing __iter__ and __next__ for an object, you can just create a generator.

PEP 525的合理性很好地说明了这一点,其中还包括一个很好的示例,显示了使用异步生成器时编写的代码所节省的费用.

This is nicely stated in the rational of PEP 525 which also includes a nice example that shows the savings you make in code written when you use async generators.

除了节省代码长度外,异步生成器的性能也更好:

In addition to code length savings, async generators also perform much better:

性能是此建议的另一个要点:在我们对参考实现的测试中,异步生成器的速度比等效实现为异步迭代器的速度快2倍.

Performance is an additional point for this proposal: in our testing of the reference implementation, asynchronous generators are 2x faster than an equivalent implemented as an asynchronous iterator.


只需在此处添加一些术语,因为有时很难跟踪术语:


Just to add some terminology here because it's getting difficult to keep track of terms sometimes:

  • 生成器:def个包含一个或多个yield表达式的函数.
  • 基于生成程序的协程:由def+ yield) rel ="nofollow noreferrer"> types.coroutine .如果需要将其视为协程对象,则需要将其包装在types.coroutine中.
  • 异步生成器:async def函数包含一个或多个yield表达式.这些还可以包含await表达式.
  • 协程:async def没有零个或多个await s,也没有yield s.
  • Generators: def functions that contain one or more yield expressions.
  • Generator-based coroutine: A generator (def + yield) that is wrapped by types.coroutine. You need to wrap it in types.coroutine if you need it to be considered a coroutine object.
  • Asynchronous Generator: async def functions that contain a one or more yield expressions. These can also contain await expressions.
  • Coroutine: async def without zero or more awaits and no yields.

这篇关于生成器功能和异步生成器功能的用途有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆