# 运行时错误：DataLoader 工作进程（pid 12345）在等待队列数据 300 秒后超时。考虑增加超时时间或减小批次大小。

- **ID:** `pytorch/dataloader-queue-timeout`
- **领域:** pytorch
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

DataLoader 工作进程在等待预取队列数据时卡住，通常是由于数据加载慢、自定义数据集中的死锁或共享内存不足。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| torch>=1.7 | active | — | — |
| torch<=2.5.1 | active | — | — |

## 解决方案

1. ```
   Increase the timeout parameter in DataLoader. For example: DataLoader(dataset, batch_size=32, num_workers=4, timeout=600) to allow up to 600 seconds.
   ```
2. ```
   Reduce batch size or number of workers to lower memory and I/O pressure. For example: DataLoader(dataset, batch_size=16, num_workers=2)
   ```
3. ```
   Use prefetch_factor=1 in DataLoader to reduce prefetch queue size: DataLoader(dataset, batch_size=32, num_workers=4, prefetch_factor=1)
   ```

## 无效尝试

- **Increasing the number of workers to speed up loading** — More workers can exacerbate shared memory contention and lead to more timeouts, especially if the bottleneck is I/O or CPU. (70% 失败率)
- **Setting timeout=0 to disable timeout** — Setting timeout=0 means no timeout, but the worker may hang indefinitely, causing the training to stall. (80% 失败率)
