如何在并行化时使用zip进行异步

2024-05-08 08:48:03 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个异步函数:

async def do_stuff(data):
   res = await result(data)
   return res

我希望能够在一堆数据输入上并行执行do_,因为结果(数据)是一个IO绑定函数

但是我还想使用数据的一些属性,比如,name属性。我该怎么做? 我所尝试的:

def run_parallel(data_list, workers):
   sem = asyncio.Semaphore(workers)
   async with sem:
      async for r in ayncio.gather([do_stuff(data) for data in data_list]):
         # How to get data.name as well from the loop? One way would be to return in do_stuff, 
         # but that is not the expected behavior of do_stuff... 
         # I though about zip(), but it's not async...
         await do_other_task(r, data.name) # <---- Expected to be able to do this

我读过关于aiostream.stream.zip的文章,但看不到如何将其插入其中并保留聚集(或在完成时)