例如我有两个功能:
async def func1(text):
asyncio.sleep(randint(1,5))
return text+'_1'
async def func2(text):
asyncio.sleep(randint(1,5))
return text+'_2'
我也有变量列表。
variables = [f'var{i}' for i in range(1, 11)]
我需要同时为每个变量运行两个函数,当 func 返回类似
for res in asyncio.as_completed([func1(text) for text in variables]): result = await res
但这样做有两个功能。结果应该是每个变量,如“['var1_1','var1_2']”
我花了这么多时间我已经尝试过疯狂的事情:
asyncio.gather(*[func1(text) for text in variables], *[func2(text) for text in variables])
asyncio.as_completed(await asyncio.gather(*[func1(text), func2(text) for text in variables]))