修正限流任务的实现,确保在调用时传递信号量参数
Some checks failed
Sync Container Images / sync-images (push) Failing after 43s
Some checks failed
Sync Container Images / sync-images (push) Failing after 43s
This commit is contained in:
parent
7b32044803
commit
4d671a283f
10
sync.py
10
sync.py
@ -55,9 +55,9 @@ async def docker_task(source: str, target: str) -> int | None:
|
|||||||
|
|
||||||
semaphore = asyncio.Semaphore(50)
|
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:
|
async with semaphore:
|
||||||
return await task()
|
return task()
|
||||||
|
|
||||||
|
|
||||||
async def main():
|
async def main():
|
||||||
@ -75,10 +75,9 @@ async def main():
|
|||||||
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, docker_task(f"{image.target}:{tag}", f"{image.target}:{tag}")))
|
||||||
tasks.append(limited_task(docker_task(f"{image.target}:{tag}", f"{image.target}:{tag}")))
|
|
||||||
else:
|
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)
|
results = await asyncio.gather(*tasks)
|
||||||
failed_tasks = 0
|
failed_tasks = 0
|
||||||
@ -88,7 +87,6 @@ async def main():
|
|||||||
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.")
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
asyncio.run(main())
|
asyncio.run(main())
|
Loading…
x
Reference in New Issue
Block a user