龙卷风:打开未来的多层

Tornado: Unpack many layers of Future

在tornado 4.3 + python3中,如果我有多层异步函数,例如:

@gen.coroutine
def layer_2():
    return(yield async_func())

@gen.coroutine 
def layer_1():
    return(yield layer_2())

@gen.coroutine
def main():
    return(yield layer_1())

由于异步函数 return 是一个 Future(生成此 Future return 的结果),因此要在 [=12] 中获取 async_func 的 returned 值=], 我必须:

有没有办法避免这种模式?

这是在Tornado中从协程调用协程的正确方法。没有 "way to avoid this pattern",实际上协程就是这样设计的!

有关详细信息,请参阅 Tornado coroutines guide or my Refactoring Tornado Coroutines