# 运行时错误：后台任务失败：'NoneType'对象没有'send'属性

- **ID:** `python/starlette-background-task-error`
- **领域:** python
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

后台任务函数未定义为async或调用不正确，导致ASGI服务器尝试运行时失败。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| 3.x | active | — | — |

## 解决方案

1. **** (95% 成功率)
   ```
   Define the task as async: async def send_email(email: str):\n    # ...\nthen use BackgroundTask(send_email, email)
   ```
2. **** (90% 成功率)
   ```
   Use Starlette's BackgroundTasks: background_tasks.add_task(send_email, email)
   ```

## 无效尝试

- **** — Calling the background task function directly instead of passing it to BackgroundTask causes immediate execution. (80% 失败率)
- **** — Making the task synchronous works in some cases but blocks the event loop. (60% 失败率)
