为什么基于生成器的协程消耗,异步生成器异步数据生产者和协程异步数据消耗者? [英] Why are generator-based coroutines consumes, asynchronous generators asynchronous data producers, and coroutines asynchronous data consumers?
问题描述
生成器:
def
函数其中包含一个或多个yield
表达式。
Generators:
def
functions that contain one or moreyield
expressions.
生成器用作数据生成器(它们产量
值)。
Generators are used as data producers (they yield
values).
我能理解。
基于生成器的协程:由
def + yield
) /docs.python.org/3/library/types.html#types.coroutine rel = nofollow noreferrer>types.coroutine
。如果需要将其视为协程对象,则需要将其包装在
types.coroutine
中。
Generator-based coroutine: A generator (
def
+yield
) that is wrapped bytypes.coroutine
. You need to wrap it intypes.coroutine
if you need it to be considered a coroutine object.
基于生成的协程用作消费者(您 .send
值是它们的
或它们是的子生成器)
消费者(您 .send
值发送给它们或子生成器,它们从
产生的收益)是什么意思?
What does "consumers (you .send
values to them or to a sub-generator they yield from
)" mean?
异步生成器:
async def
包含一个或多个<$ c的函数$ c> yield 表达式。这些还可以包含await
表达式。
Asynchronous Generator:
async def
functions that contain a one or moreyield
expressions. These can also containawait
expressions.
异步生成器是异步数据生成器。
异步数据生成器是什么意思?
What does "asynchronous data producers" mean?
协程:
async def
没有零个或多个await
s且没有收益率
s。
Coroutine:
async def
without zero or moreawait
s and noyield
s.
协程是异步数据使用者。
异步数据使用者是什么意思?
What does "asynchronous data consumers" mean?
谢谢。
推荐答案
在python中,现在以多种方式使用生成器。 生成器的最初目的是暂停执行,然后将一个值 yield
返回给调用方。然后,呼叫者可以稍后再呼叫以恢复生成器。因此,生成器就是数据生成器。
In python, generators are NOW used in a number of various ways. The original purpose of generators was to suspend execution and then yield
a value back to the caller. The caller can then call next later to resume the generator. Hence generators were data producers.
现在,上述版本的生成器仅允许通过 yield
语句返回数据。现在,要使函数成为协程,它还应该接受调用方的值。因此在python 2.5中引入了 PEP 342 增强生成器,使其可以充当完整的协程。
Now the above version of generators only allowed returning data through the yield
statement. Now for a function to be a coroutine, it should also accept values from the caller. Hence PEP 342 was introduced in python 2.5 to enhance generators so that they can act as full fledged coroutines. This allowed callers to send values to the generators.
现在,新问题是,当重构生成器并且您想将其操作的一部分委托给子生成器时,您需要显式调用子生成器作为迭代器,传播调用方发送的数据并处理异常。为了简化子生成器的操作,在收益 pep-0380 / rel = nofollow noreferrer> PEP 380 作为python 3.3的一部分。从语法上说,
的
yield比普通的yield语法还重要。在一个完美的世界中,可能会使用一个新的关键字。
Now the new issue was that when generators were refactored and you wanted to delegate parts of its operation to subgenerators, you need to explicitly invoke the subgenerator as an iterator, propagate the data send by caller and handle exception. To simplify the operation of subgenerators, a new operation yield from
was defined in PEP 380 as part of python 3.3. The yield from
is syntactically is much more than the plain yield syntax. In a perfect world, a new keyword probably would have used.
现在的问题是,生成器在两个不同的上下文中使用。作为迭代器和协程。如果可以将生成器明确定义为协程,那就更好了。因此 PEP 492引入了 async $
和 await
关键字。因此, async
关键字指示了用作协程的任何生成器。 Python 3.5中的协程可以使用 await
关键字代替 的收益。请注意,从python 3.5开始,协程类型是不同的类型!!
Now the issue was that generators were used in two different contexts. As an iterator and as a coroutine. It would have been better if a generator can be explicitly defined as a coroutine. Hence PEP 492 introduced async
and await
keywords in Python 3.5. Hence any generator which was used as a coroutine was indicated by the async
keyword. The coroutine in Python 3.5 can use await
keyword instead of the yield from
. Note that from python 3.5 onwards coroutines are a different type!!
现在假设您有一个带有 def
的生成器函数和收益
。您可以使用 types.coroutine
装饰器将现有的生成器类型转换为协程类型。这些消费者可以通过 send()
接受值,并使用来自的
收益将其委托给子生成器。
Now assume you have a generator function with def
and yield
. You can convert an existing generator type to a coroutine type using the types.coroutine
decorator. These are consumers who can accept values through send()
and delegate the same to subgenerators using yield from
.
在python 3.5中,可以使用 async
表示该函数是协程类型。这样的函数可以包含普通的 yield
和 await
。它们不能包含来自$code的收益(因为
await
会替换该功能)。当协程包含简单的 yield
时,它们在生成器调用链中是最低的,因此称为异步数据生成器。
In python 3.5, you can use async
to indicate that the function is a coroutine type. Such a function can contain plain yield
and await
. They cannot contain yield from
(since await
replaces the feature). When a coroutine contains plain yield
, they are the lowest in a chain of generator calls and hence called an asynchronous data producer.
任何没有简单的收益协程>的协程将成为数据使用者,因为它必须通过
等待
以获取异步数据。
Any coroutine without plain yield
will be a data consumer since it must call another coroutine through await
to get asynchronous data.
这篇关于为什么基于生成器的协程消耗,异步生成器异步数据生产者和协程异步数据消耗者?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!