异步/等待做什么? [英] What does async/await do?
问题描述
我正在尝试用python绕过异步/等待.
I'm trying to wrap my head around async/await in python.
我在正确的轨道上吗?
-
async
和@coroutine
函数返回协程/生成器,而不是返回值. -
await
提取协程/发电机的实际返回值.
async
and@coroutine
functions returns coroutine/generator, not the returned value.await
extracts the actual return value of coroutine/generator.
async
函数结果(协程)应添加到事件循环中.
async
function result (coroutines) is meant to be added to event-loop.
@coroutine
的yield
直接与事件循环通信. (跳过等待结果的直接呼叫者)
@coroutine
's yield
communicates directly with event-loop. (skipping direct caller which awaits the result)
await
仅可在异步函数内使用.
await
can be used only inside async functions.
(@coroutine
= @types.coroutine
)
推荐答案
async
和@coroutine
函数返回协程/生成器,而不是返回值
async
and@coroutine
functions returns coroutine/generator, not the returned value
从技术上讲,types.coroutine
返回基于生成器的协程,该协程不同于生成器,也不同于协程.
To be technical, types.coroutine
returns a generator-based coroutine which is different than generators and different than coroutines.
await提取协程/发电机的实际返回值.
await extracts the actual return value of coroutine/generator.
await
,类似于yield from
,暂停协程的执行,直到等待完成并返回结果为止.
await
, similar to yield from
, suspends the execution of the coroutine until the awaitable it takes completes and returns the result.
异步功能结果(协程)应添加到事件循环中.
async function result (coroutines) is meant to be added to event-loop.
是的.
await在事件循环和等待的协程之间建立桥梁"(启用下一个点).
await creates "bridge" between event-loop and awaited coroutine (enabling the next point).
await创建一个挂起点,该挂起点指示事件循环某些I/O操作将发生,从而允许它切换到另一个任务.
await creates a suspension point that indicates to the event loop that some I/O operation will take place thereby allowing it to switch to another task.
@协程的产量直接与事件循环通信. (跳过等待结果的直接呼叫者)
@coroutine's yield communicates directly with event-loop. (skipping direct caller which awaits the result)
不,基于生成器的协程使用yield from
的方式与await
相似,而不是yield
.
No, generator-based coroutines use yield from
in a similar fashion to await
, not yield
.
await只能在异步函数中使用.
await can be used only inside async functions.
是的
产量只能在协程内部使用.
yield can be used only inside coroutine.
yield from
可以在基于生成器的协程(用types.coroutine
装饰的生成器)内部使用,并且自Python 3.6起,可以在async
函数中使用,从而生成异步生成器.
yield from
can be used inside generator-based coroutines (generators decorated with types.coroutine
) and, since Python 3.6, in async
functions that result in an asynchronous generator.
这篇关于异步/等待做什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!