python - tornado异步队列 - yielded unknown object



tornado.gen.engine v/s tornado.gen.coroutine中的区别 (1)

正如gen.engine的龙卷风文档所说:

这个装饰器类似于协程,除非它不返回一个Future,并且回调参数没有被特别处理。

正如gen.coroutine文档所说的那样

从调用者的角度来看,@ gen.coroutine类似于@return_future和@ gen.engine的组合。

gen.engine基本上是一个较老的,简化版本的协程。 如果你正在编写新的代码,你应该遵循文档的建议,并始终使用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

协程:

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

        if 'callback' in kwargs:
            callback = kwargs.pop('callback')
            IOLoop.current().add_future(
                future, lambda future: callback(future.result()))

        def handle_exception(typ, value, tb):
            try:
                if runner is not None and runner.handle_exception(typ, value, tb):
                    return True
            except Exception:
                typ, value, tb = sys.exc_info()
            future.set_exc_info((typ, value, tb))
            return True
        with ExceptionStackContext(handle_exception) as deactivate:
            try:
                result = func(*args, **kwargs)
            except (Return, StopIteration) as e:
                result = getattr(e, 'value', None)
            except Exception:
                deactivate()
                future.set_exc_info(sys.exc_info())
                return future
            else:
                if isinstance(result, types.GeneratorType):
                    def final_callback(value):
                        deactivate()
                        future.set_result(value)
                    runner = Runner(result, final_callback)
                    runner.run()
                    return future
            deactivate()
            future.set_result(result)
        return future
    return wrapper

乍一看,这些都可能很难理解。 但是,显然代码是非常相似的,只是@gen.coroutinecallback kwarg有一些特殊的处理,并且构建/返回一个Future@gen.engine有一些代码,如果你试图从它返回一些东西,而不是把它放在Future ,那么就会抛出一个错误。

去tornado.gen 文档可以帮助我了解tornado.gen.coroutine和tornado.gen.engine之间的确切区别





tornado