From 4d671a283f659ac07402d164c033e6da120110ce Mon Sep 17 00:00:00 2001 From: Snowykami Date: Thu, 17 Apr 2025 22:07:06 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=AD=A3=E9=99=90=E6=B5=81=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1=E7=9A=84=E5=AE=9E=E7=8E=B0=EF=BC=8C=E7=A1=AE=E4=BF=9D?= =?UTF-8?q?=E5=9C=A8=E8=B0=83=E7=94=A8=E6=97=B6=E4=BC=A0=E9=80=92=E4=BF=A1?= =?UTF-8?q?=E5=8F=B7=E9=87=8F=E5=8F=82=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sync.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/sync.py b/sync.py index 7c1b1bd..a8df6d0 100644 --- a/sync.py +++ b/sync.py @@ -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()) \ No newline at end of file