这两种基于生成器的协程是否具有相同的概念? [英] Are these two kinds of generator-based coroutines the same concept?

查看:79
本文介绍了这两种基于生成器的协程是否具有相同的概念?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

似乎有两种基于生成器的协程:

There seem to be two kinds of generator-based coroutine:


  1. 来自Jim Fasarakis Hilliard的
    答复


基于发电机的协程:包装的发电机( def + 产量)由 types.coroutine 。如果需要将其视为协程对象,则需要将其包装在
types.coroutine 中。

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.


  • 来自Python的果壳,没有明确地将其称为
    基于发电机的协程:

  • From Python in a Nutshell, which doesn't explicitly call it "generator-based coroutine":

    当您基于 asyncio 编写Python代码时(最好也使用asyncio.org的
    附加模块),通常会编写
    协程函数。直到包含Python 3.4,此类函数
    都是生成器,使用第95页的 $ yield
    from(v3-only)中包含的 yield from 语句,用 @ asyncio.coroutine 装饰,在$ 518上的 asyncio协程中覆盖了

    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


    基于生成器的协程(对于异步代码,必须使用@ asyncio.coroutine进行修饰)

    generator-based coroutines (for asyncio code must be decorated with @asyncio.coroutine)

    http://masnun.com/2015/11/13/python- generators-coroutines-native-coroutines-async-await.html
    也称其为基于发电机的协程。

    http://masnun.com/2015/11/13/python-generators-coroutines-native-coroutines-and-async-await.html also calls it "generator-based coroutine".

    是两种基于指令的协同程序是否具有相同的概念?

    Are the two kinds of generator-based coroutines the same concept?

    如果不是,它们在用途和用法上有什么区别?

    If not, what are their differences in purposes and usages?

    谢谢。

    推荐答案

    它们是同一种协程。 types.coroutine asyncio.coroutine 只是创建它们的两种单独方法。

    They're the same kind of coroutine. types.coroutine and asyncio.coroutine are just two separate ways to create them.

    asyncio.coroutine 较旧,早于 async 协程的引入,其功能具有现在已经存在 async 协程。

    asyncio.coroutine is older, predating the introduction of async coroutines, and its functionality has shifted somewhat from its original behavior now that async coroutines exist.

    asyncio.coroutine types.coroutine 的行为略有不同,尤其是应用于生成器函数以外的任何东西,或者asyncio位于调试模式

    asyncio.coroutine and types.coroutine have subtly different behavior, especially if applied to anything other than a generator function, or if asyncio is in debug mode.

    这篇关于这两种基于生成器的协程是否具有相同的概念?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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