假设我有 26 个任务要并行运行。为了最大限度地减少服务器上的负载,我决定一次运行 10 个任务:首先并行运行 10 个任务,然后是接下来的 10 个任务,最后是剩余的 6 个任务。
我编写了一个简单的脚本来实现此行为:
import asyncio
from string import ascii_uppercase
from typing import List
TASK_NAMES = ascii_uppercase # 26 fake tasks in total
class BatchWorker:
"""Run a list of tasks in batch."""
BATCH_SIZE = 10
def __init__(self, tasks: List[asyncio.Task]):
self._tasks = list(tasks)
@property
def batch_of_tasks(self):
"""Yield all tasks by chunks of `BATCH_SIZE`"""
start = 0
while 'there are items remaining in the list':
end = start + self.BATCH_SIZE
chunk = self._tasks[start:end] …Run Code Online (Sandbox Code Playgroud)