这两种基于生成器的协程是同一个概念吗?
Are these two kinds of generator-based coroutines the same concept?
基于生成器的协程好像有两种:
来自一个
回复
吉姆·法萨拉基斯·希利亚德 (Jim Fasarakis Hilliard):
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.
From Python 简而言之,没有明确调用它
"generator-based coroutine":
When you write Python code based on asyncio
(ideally also using
add-on modules from asyncio.org), you’ll usually be writing
coroutine functions. Up to Python 3.4 included, such functions
are generators using the yield from
statement covered in “yield
from (v3-only)” on page 95, decorated with @asyncio.coroutine
,
covered in “asyncio coroutines” on page 518;
来自
https://www.python.org/dev/peps/pep-0492/#differences-from-generators
generator-based coroutines (for asyncio code must be decorated with @asyncio.coroutine)
http://masnun.com/2015/11/13/python-generators-coroutines-native-coroutines-and-async-await.html
也称为 "generator-based coroutine".
这两种基于生成器的协程是同一个概念吗?
如果不是,它们在用途和用法上有什么区别?
谢谢。
就我而言,async def
是定义协程的 正确 方式。 yield
和 yield from
在生成器中有它们的用途,它们也用于实现“futures”,这是处理不同协程上下文之间切换的低级机制。
我 this diagram a few months ago to summarize the relationships between them. But frankly, you can safely ignore the whole business. Event loops have the job of handling all the low-level details of managing the execution of coroutines, so use one of those, like asyncio. There are also asyncio
-compatible wrappers for other event loops, like my own glibcoro
GLib/GTK。
换句话说,坚持 asyncio
API,你就可以编写“事件循环无关”的协程!
它们是同一种协程。 types.coroutine
和 asyncio.coroutine
只是创建它们的两种不同方式。
asyncio.coroutine
较旧,早于 async
协同程序的引入,并且由于存在 async
协同程序,其功能已从其原始行为发生了一些变化。
asyncio.coroutine
和 types.coroutine
的行为略有不同,尤其是应用于生成器函数以外的任何东西时,或者如果 asyncio 在 debug mode.
中
基于生成器的协程好像有两种:
来自一个 回复 吉姆·法萨拉基斯·希利亚德 (Jim Fasarakis Hilliard):
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.From Python 简而言之,没有明确调用它 "generator-based coroutine":
When you write Python code based on
asyncio
(ideally also using add-on modules from asyncio.org), you’ll usually be writing coroutine functions. Up to Python 3.4 included, such functions are generators using theyield from
statement covered in “yield from (v3-only)” on page 95, decorated with@asyncio.coroutine
, covered in “asyncio coroutines” on page 518;来自 https://www.python.org/dev/peps/pep-0492/#differences-from-generators
generator-based coroutines (for asyncio code must be decorated with @asyncio.coroutine)
http://masnun.com/2015/11/13/python-generators-coroutines-native-coroutines-and-async-await.html 也称为 "generator-based coroutine".
这两种基于生成器的协程是同一个概念吗?
如果不是,它们在用途和用法上有什么区别?
谢谢。
就我而言,async def
是定义协程的 正确 方式。 yield
和 yield from
在生成器中有它们的用途,它们也用于实现“futures”,这是处理不同协程上下文之间切换的低级机制。
我 this diagram a few months ago to summarize the relationships between them. But frankly, you can safely ignore the whole business. Event loops have the job of handling all the low-level details of managing the execution of coroutines, so use one of those, like asyncio. There are also asyncio
-compatible wrappers for other event loops, like my own glibcoro
GLib/GTK。
换句话说,坚持 asyncio
API,你就可以编写“事件循环无关”的协程!
它们是同一种协程。 types.coroutine
和 asyncio.coroutine
只是创建它们的两种不同方式。
asyncio.coroutine
较旧,早于 async
协同程序的引入,并且由于存在 async
协同程序,其功能已从其原始行为发生了一些变化。
asyncio.coroutine
和 types.coroutine
的行为略有不同,尤其是应用于生成器函数以外的任何东西时,或者如果 asyncio 在 debug mode.