差异龙卷风发电机垂直速度龙卷风发电机协同程序

2024-10-01 15:31:49 发布

您现在位置:Python中文网/ 问答频道 /正文

经过龙卷风.gendocumentation有人能帮我理解这两者之间的确切区别吗龙卷风发电机协同程序以及龙卷风发电机在


Tags: 发电机区别协同程序gendocumentation
1条回答
网友
1楼 · 发布于 2024-10-01 15:31:49

正如gen.engine的tornado文档所述:

This decorator is similar to coroutine, except it does not return a Future and the callback argument is not treated specially.

正如gen.coroutine文档所说

From the caller’s perspective, @gen.coroutine is similar to the combination of @return_future and @gen.engine.

gen.engine基本上是coroutine的一个较老的、不太精简的版本。如果您正在编写新代码,您应该遵循文档的建议并始终使用tornado.gen.coroutine。在

如果您查看这两个函数的代码(去掉文档),这一点非常明显。在

发动机:

def engine(func):
    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        runner = None

        def handle_exception(typ, value, tb):
            if runner is not None:
                return runner.handle_exception(typ, value, tb)
            return False
        with ExceptionStackContext(handle_exception) as deactivate:
            try:
                result = func(*args, **kwargs)
            except (Return, StopIteration) as e:
                result = getattr(e, 'value', None)
            else:
                if isinstance(result, types.GeneratorType):
                    def final_callback(value):
                        if value is not None:
                            raise ReturnValueIgnoredError(
                                "@gen.engine functions cannot return values: "
                                "%r" % (value,))
                        assert value is None
                        deactivate()
                    runner = Runner(result, final_callback)
                    runner.run()
                    return
            if result is not None:
                raise ReturnValueIgnoredError(
                    "@gen.engine functions cannot return values: %r" %
                    (result,))
            deactivate()
            # no yield, so we're done
    return wrapper

合作路线:

^{pr2}$

乍一看,这两个可能都很难理解。{{cd6}很明显,{cd6}的处理仍然很相似。@gen.engine有一些代码,如果您试图从中返回某些内容,而不是将其放入Future中,则会特别抛出一个错误。在

相关问题 更多 >

    热门问题