优化异步任务处理,修复任务传递和返回值处理逻辑
All checks were successful
Sync Container Images / sync-images (push) Successful in 49s

This commit is contained in:
远野千束 2025-04-17 22:10:56 +08:00
parent 4d671a283f
commit 82517a10a7

14
sync.py
View File

@ -12,7 +12,6 @@ class Images(BaseModel):
target: str target: str
tags: list[str] = [] tags: list[str] = []
async def run_command(command: str) -> int | None: async def run_command(command: str) -> int | None:
process = await asyncio.create_subprocess_shell( process = await asyncio.create_subprocess_shell(
command, command,
@ -57,8 +56,7 @@ semaphore = asyncio.Semaphore(50)
async def limited_task[T: Any](semaphore: asyncio.Semaphore, 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: async with semaphore:
return task() return await task()
async def main(): async def main():
async with aiofiles.open('images.yaml', 'r') as file: async with aiofiles.open('images.yaml', 'r') as file:
@ -74,16 +72,12 @@ async def main():
for image in config.images: for image in config.images:
if len(image.tags) > 0: if len(image.tags) > 0:
for tag in image.tags: for tag in image.tags:
tasks.append(limited_task(semaphore, lambda: docker_task(f"{image.source}:{tag}", f"{image.target}:{tag}")))
tasks.append(limited_task(semaphore, docker_task(f"{image.target}:{tag}", f"{image.target}:{tag}")))
else: else:
tasks.append(limited_task(semaphore, docker_task(image.source, image.target))) tasks.append(limited_task(semaphore, lambda: docker_task(image.source, image.target)))
results = await asyncio.gather(*tasks) results = await asyncio.gather(*tasks)
failed_tasks = 0 failed_tasks = sum(1 for result in results if result is not None and result != 0)
for result in results:
if result is not None and result != 0:
failed_tasks += 1
print(f"{len(results)} tasks completed. {len(results) - failed_tasks} succeed, {failed_tasks} failed.") print(f"{len(results)} tasks completed. {len(results) - failed_tasks} succeed, {failed_tasks} failed.")
if failed_tasks > 0: if failed_tasks > 0:
raise Exception(f"{failed_tasks} tasks failed.") raise Exception(f"{failed_tasks} tasks failed.")