@ types.coroutine和@ asyncio.coroutine装饰器有什么区别? [英] What is the difference between @types.coroutine and @asyncio.coroutine decorators?

查看:208
本文介绍了@ types.coroutine和@ asyncio.coroutine装饰器有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

文档说:

@ asyncio.coroutine

@asyncio.coroutine

装饰器,用于标记基于生成器的协程.这使生成器可以使用yield from来调用异步def协程,并且 使生成器可以被异步def协程调用, 使用await表达式的实例.

Decorator to mark generator-based coroutines. This enables the generator use yield from to call async def coroutines, and also enables the generator to be called by async def coroutines, for instance using an await expression.

_

@ types.coroutine(gen_func)

@types.coroutine(gen_func)

此函数可转换生成器 函数转换为协程函数,该函数返回基于生成器的 协程.基于生成器的协程仍然是生成器 迭代器,但也被视为协程对象,并且 值得期待.但是,它不一定实现__await__() 方法.

This function transforms a generator function into a coroutine function which returns a generator-based coroutine. The generator-based coroutine is still a generator iterator, but is also considered to be a coroutine object and is awaitable. However, it may not necessarily implement the __await__() method.

似乎目的是相同的-将生成器标记为协程(Python3.5及更高版本中的async def具有某些功能).

So is seems like purposes is the same - to flag a generator as a coroutine (what async defin Python3.5 and higher does with some features).

何时需要使用asyncio.coroutine何时需要使用types.coroutine,差异是什么?

When need to use asyncio.coroutine when need to use types.coroutine, what is the diffrence?

推荐答案

不同之处在于是否具有yield语句. 这是代码:

The difference is if you have a yield statement or not. Here's the code:

from types import coroutine as t_coroutine
from asyncio import coroutine as a_coroutine, ensure_future, sleep, get_event_loop


@a_coroutine
def a_sleep():
    print("doing something in async")
    yield 1


@t_coroutine
def t_sleep():
    print("doing something in types")
    yield 1


async def start():
    sleep_a = a_sleep()
    sleep_t = t_sleep()
    print("Going down!")


loop = get_event_loop()
loop.run_until_complete(start())

在此示例中,所有内容似乎都相同-这是pycharm的调试信息(我们站在往下走!"行).控制台上尚未打印任何内容,因此功能尚未启动.

In this example everything seem same - here's the debugging info from pycharm (we're standing on the "Going down!" line). Nothing is printed in console yet, so the functions didn't start yet.

但是如果我们删除yield ,则types版本将立即启动功能!

But if we remove the yield, the types version will start function instantly!

from types import coroutine as t_coroutine
from asyncio import coroutine as a_coroutine, ensure_future, sleep, get_event_loop


@a_coroutine
def a_sleep():
    print("doing something in async")


@t_coroutine
def t_sleep():
    print("doing something in types")


async def start():
    sleep_a = a_sleep()
    sleep_t = t_sleep()
    print("Going down!")


loop = get_event_loop()
loop.run_until_complete(start())

现在我们在控制台中打印了doing something in types.这是调试信息:

Now we have doing something in types in console printed. And here's the debug info:

如您所见,呼叫后立即开始,如果没有结果,则返回无.

As you can see it starts right after call, if there is no result and returns None.

至于用法,应始终使用asyncio版本.如果您需要像 fireand忘记这样运行(立即运行并稍后获得结果),请使用ensure_future函数.

As for usage, you should use asyncio version always. If you need to run it like fire and forget (running instantly and getting results later) - use ensure_future function.

这篇关于@ types.coroutine和@ asyncio.coroutine装饰器有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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