import asyncio
import sys

async def execute_with_retry(task, max_retries, delay):
    attempts = 0

    while attempts < max_retries:
        try:
            return await task()
        except Exception as error:
            attempts += 1
            print(f"尝试 {attempts} 失败: {error}", file=sys.stderr)

            if attempts >= max_retries:
                print("已达最大重试次数，任务失败。", file=sys.stderr)
                raise

            print(f"等待 {delay} 秒后重试...")
            await asyncio.sleep(delay)


__all__ = ["execute_with_retry"]