> For the complete documentation index, see [llms.txt](https://docs.unitlab.ai/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.unitlab.ai/api-sdk-cli/zh/sdks/batch-queues-and-processing.md).

# Batch Queue 与处理状态

使用 Unitlab Python SDK 的 Batch Queue 跟踪上传在服务器端的处理进度，带超时等待完成，并列出处理失败的文件。

每次上传、云端导入和关联都会创建一个 **Batch Queue**。服务器在后台处理其中的文件：读取元数据、抽取视频帧，并为大型栅格图生成切片。

```python
queue = project.batch_queue("BATCH_QUEUE_ID")
status = queue.wait(timeout=1800, show_progress=True)

if status.failed:
    for row in queue.data():
        print(row)
```

## 方法 <a href="#methods" id="methods"></a>

| 调用                                                                | 返回                     |
| ----------------------------------------------------------------- | ---------------------- |
| `project.batch_queues()`                                          | 项目的所有 Batch Queue      |
| `project.batch_queue(id)`                                         | 单个 `BatchQueue`        |
| `queue.status()`                                                  | 当前的 `ProcessingStatus` |
| `queue.wait(timeout=1800, on_progress=None, show_progress=False)` | 最终的 `ProcessingStatus` |
| `queue.data()`                                                    | 队列中的文件及其处理状态           |
| `queue.refresh()`                                                 | 重新读取的队列                |

`project.upload()` 返回的 `UploadBatch.wait()` 功能相同，并会在终端中显示进度条。

## ProcessingStatus <a href="#processingstatus" id="processingstatus"></a>

| 字段                                           | 含义                                                    |
| -------------------------------------------- | ----------------------------------------------------- |
| `status`                                     | `processing`、`completed`、`partial_failure` 或 `failed` |
| `total`, `completed`, `processing`, `failed` | 文件计数                                                  |
| `error`                                      | 整个队列失败时的信息                                            |
| `frame_count`                                | LiDAR 场景的帧数，其他情况为 `None`                              |

当 `processing` 为 0 时，`wait()` 结束。轮询间隔从 1 秒开始，逐步退避到 20 秒。

## 进度回调 <a href="#progress-callbacks" id="progress-callbacks"></a>

```python
def report(status):
    print(f"{status.completed}/{status.total} done, {status.failed} failed")

queue.wait(timeout=3600, on_progress=report)
```

## 等待超时时 <a href="#when-the-wait-times-out" id="when-the-wait-times-out"></a>

`ProcessingTimeoutError` 表示处理仍在进行，而不是处理失败。最后一次状态保存在 `error.status` 中。请保留 Batch Queue ID，稍后再次等待：

```python
from unitlab import ProcessingTimeoutError

try:
    queue.wait(timeout=600)
except ProcessingTimeoutError as error:
    print("Still processing:", error.status.processing)
```

## 相关内容 <a href="#related" id="related"></a>

[项目上传](https://docs.unitlab.ai/api-sdk-cli/zh/sdks/project-uploads.md) · [CLI：Batch Queue](https://docs.unitlab.ai/api-sdk-cli/zh/clis/cli-batch-queues.md) · [异常、超时与重试](https://docs.unitlab.ai/api-sdk-cli/zh/sdks/exceptions-timeouts-and-retries.md)
