我想有条件地运行异步函数,如下所示:
one, two, three = await asyncio.gather(
some_async_method1(),
some_async_method2() if some_condition else None, # None doesn't work here
some_async_method3()
)
当 some_condition
为 False
时,我预计 two
为 None
。如何做到这一点?
最佳答案
您可以创建一个函数
async def await_none():
return None
然后将代码更改为
some_async_method2() if some_condition else await_none(),
可能有一种更简单的方法可以做到这一点。
关于python - 如何在 asyncio.gather 中使用条件逻辑?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73587367/