修正限流任务的实现,确保在调用时传递信号量参数
Some checks failed
Sync Container Images / sync-images (push) Failing after 43s

This commit is contained in:
远野千束 2025-04-17 22:07:06 +08:00
parent 7b32044803
commit 4d671a283f

10
sync.py
View File

@ -55,9 +55,9 @@ async def docker_task(source: str, target: str) -> int | None:
semaphore = asyncio.Semaphore(50)
async def limited_task[T: Any](task: Callable[[], Coroutine[None, None, T]]) -> T:
async def limited_task[T: Any](semaphore: asyncio.Semaphore, task: Callable[[], Coroutine[None, None, T]]) -> T:
async with semaphore:
return await task()
return task()
async def main():
@ -75,10 +75,9 @@ async def main():
if len(image.tags) > 0:
for tag in image.tags:
# 将协程对象添加到任务列表
tasks.append(limited_task(docker_task(f"{image.target}:{tag}", f"{image.target}:{tag}")))
tasks.append(limited_task(semaphore, docker_task(f"{image.target}:{tag}", f"{image.target}:{tag}")))
else:
tasks.append(limited_task(docker_task(image.source, image.target)))
tasks.append(limited_task(semaphore, docker_task(image.source, image.target)))
results = await asyncio.gather(*tasks)
failed_tasks = 0
@ -88,7 +87,6 @@ async def main():
print(f"{len(results)} tasks completed. {len(results) - failed_tasks} succeed, {failed_tasks} failed.")
if failed_tasks > 0:
raise Exception(f"{failed_tasks} tasks failed.")
if __name__ == "__main__":
asyncio.run(main())