> ## Documentation Index
> Fetch the complete documentation index at: https://docs.yingtu.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# 轮询异步图片任务

> 使用有上限的退避策略查询异步任务，直到 Nano Banana 生成完成或失败。

先短暂等待，再逐步增加间隔，并在达到应用自己的截止时间后停止。
`queued` 和 `in_progress` 都是非终态。

保存任务 ID 时，同时记录模型、请求尺寸、业务任务和应用自己的截止时间。任务查询响应会返回
模型，但无法恢复客户端没有保存的业务上下文。

```python theme={"system"}
import os
import time
import requests

delay = 2.0
while True:
    response = requests.get(
        f"https://api.yingtu.ai/v1/tasks/{task_id}",
        headers={"Authorization": f"Bearer {os.environ['YINGTU_API_KEY']}"},
        timeout=30,
    )
    response.raise_for_status()
    task = response.json()
    if task["status"] in {"completed", "failed"}:
        break
    time.sleep(delay)
    delay = min(delay * 1.5, 10)
```

遇到 HTTP `429` 时，如果响应带有 `Retry-After`，请按其等待。高并发任务还应加入随机抖动，
避免所有工作进程同时轮询。

<Info>
  任务可能在提交响应的 `queued` 与第一次查询之间已经进入 `in_progress`。
  客户端不应要求观察到每一个状态，也不要根据进度字符串推算剩余时间。
</Info>

状态为 `completed` 时，遍历 `result.candidates[].content.parts[]` 查找 `inlineData`；
状态为 `failed` 时，展示 `error.message`，再判断是否需要新建一个独立任务。

<Card title="打开任务查询 Playground" icon="play" href="/cn/api-reference/get-task">
  查看各任务状态和完成结果的结构。
</Card>
