# asyncio.exceptions.InvalidStateError: 任务尚未完成

- **ID:** `python/asyncio-queue-task-done-never-set`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

在任务完成之前调用了 task.result() 或 task.exception()，二者都要求任务处于 DONE 状态。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.8+ | active | — | — |
| 3.9+ | active | — | — |
| 3.10+ | active | — | — |
| 3.11+ | active | — | — |
| 3.12+ | active | — | — |

## 解决方案

1. **** (97% 成功率)
   ```
   task = asyncio.create_task(work())
result = await task
   ```
2. **** (92% 成功率)
   ```
   done, pending = await asyncio.wait({task}, timeout=10)
if task in done:
    print(task.result())
   ```

## 无效尝试

- **** — Arbitrary sleeps are racy; under load the task may still be pending. (80% 失败率)
- **** — Busy-looping until done wastes CPU and does not guarantee completion. (70% 失败率)
