如何一次运行X中的任务列表?并等待每组结束

问题描述 投票:0回答:0

我有一组 1000 个任务。

import asyncio

async def my_task(x):
    await asyncio.sleep(0.1)
    print(f"done: {x}")


async def main():
    my_tasks = []
    for x in range(1000):
        my_tasks.append(lambda: my_task)         

    # ???
    # how to scoop up the consequent 10 out of `my_tasks`
    # to execute them asyncronously?
    # and then wait for them?
    # 

    # ??
    # asyncio.create_task(my_task())
    # pending = asyncio.all_tasks()
    # group = asyncio.gather(*pending, return_exceptions=True)
    # await group

我想运行它们 10 x 10. 即一次运行 10 个。然后等待它们 (10) 完成,然后再运行另外 10 个,依此类推。

怎么做?

python asynchronous parallel-processing python-asyncio
© www.soinside.com 2019 - 2024. All rights reserved.