修正限流机制,将信号量从 50 更改为 5,并优化任务传递逻辑以确保正确的参数传递
All checks were successful
Sync Container Images / sync-images (push) Successful in 2m47s
All checks were successful
Sync Container Images / sync-images (push) Successful in 2m47s
This commit is contained in:
parent
82517a10a7
commit
104a6822bc
7
sync.py
7
sync.py
@ -52,12 +52,13 @@ async def docker_task(source: str, target: str) -> int | None:
|
|||||||
return r
|
return r
|
||||||
return 0
|
return 0
|
||||||
|
|
||||||
semaphore = asyncio.Semaphore(50)
|
semaphore = asyncio.Semaphore(5)
|
||||||
|
|
||||||
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 await 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:
|
||||||
config = await file.read()
|
config = await file.read()
|
||||||
@ -72,9 +73,9 @@ 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, lambda s=f"{image.source}:{tag}", t=f"{image.target}:{tag}": docker_task(s, t)))
|
||||||
else:
|
else:
|
||||||
tasks.append(limited_task(semaphore, lambda: docker_task(image.source, image.target)))
|
tasks.append(limited_task(semaphore, lambda s=image.source, t=image.target: docker_task(s, t)))
|
||||||
|
|
||||||
results = await asyncio.gather(*tasks)
|
results = await asyncio.gather(*tasks)
|
||||||
failed_tasks = sum(1 for result in results if result is not None and result != 0)
|
failed_tasks = sum(1 for result in results if result is not None and result != 0)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user