【发布时间】:2020-12-20 11:19:26
【问题描述】:
我有一个函数
async def hello(a):
await asyncio.sleep(2)
print(a)
我想为列表中的几个元素异步调用这个函数
list = ['words','I','need','to','print','parallely']
类似这样,但下面的代码一个接一个地运行。
for word in list:
await hello(word)
有没有更好的方法来做到这一点?我用过asyncio.create_task,但不知道如何在循环中使用它们。
【问题讨论】:
-
不要使用
list作为变量名,它会影响内置类型list。
标签: python asynchronous python-asyncio