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