生成器/异步lambda

时间:2019-08-27 08:15:32

标签: python

有更漂亮的方法吗?

asyncio.coroutine(lambda: None)()

我想获得一个虚拟协程(生成器对象),以在if参数列表内的单行gather表达式中使用,例如

data = asyncio.gather(
    some_coro() if some_condition else asyncio.coroutine(lambda: None)(),
    # ...more of this
)

为了保留结果索引:     data[0]data[1] ...

1 个答案:

答案 0 :(得分:0)

您可以使用asyncio.sleep

data = asyncio.gather(
    some_coro() if some_condition else asyncio.sleep(0),
    # ...more of this
)

asyncio.sleep也具有result kw属性,如果您想返回特定的内容而不是None