第一章:后台任务实现原理与实战
1.1 后台任务运行机制
FastAPI通过BackgroundTasks模块实现异步后台任务处理。该机制基于Starlette的BackgroundTask实现,具有以下特点:
- 任务执行与请求处理完全解耦
- 支持依赖注入系统
- 自动处理任务异常
- 任务队列采用内存存储(适用于中小型应用)
graph TD
A[FastAPI请求] --> B{包含BackgroundTasks参数}
B -->|是| C[主线程响应]
C --> D[立即返回客户端响应]
B -->|否| E[常规请求处理]
D --> F[后台任务队列]
F --> G[异步执行任务]
G --> H[成功完成]
G --> I[失败处理]
I --> J[异常记录]
H --> K[任务状态更新]
style F fill:#9f9,stroke:#333
style G fill:#f99,stroke:#333
1.2 邮件通知实战
安装依赖:
bash
pip install fastapi==0.68.0 pydantic==1.10.7 python-dotenv==0.19.0 aiosmtplib==1.1.6
示例代码:
python
from fastapi import BackgroundTasks, FastAPI
from pydantic import BaseModel
import aiosmtplib
import os
from dotenv import load_dotenv
load_dotenv()
app = FastAPI()
class EmailRequest(BaseModel):
recipient: str
subject: str
content: str
async def send_email(recipient: str, subject: str, content: str):
"""异步发送邮件核心逻辑"""
message = f"From: {os.getenv('SMTP_USER')}\nTo: {recipient}\nSubject: {subject}\n\n{content}"
await aiosmtplib.send(
message,
hostname=os.getenv('SMTP_HOST'),
port=os.getenv('SMTP_PORT'),
username=os.getenv('SMTP_USER'),
password=os.getenv('SMTP_PASS'),
use_tls=True
)
@app.post("/send-notification")
async def send_notification(
email_data: EmailRequest,
background_tasks: BackgroundTasks
):
"""用户注册通知接口"""
background_tasks.add_task(
send_email,
email_data.recipient,
"新用户注册通知",
email_data.content
)
return {"message": "通知已加入发送队列"}
1.3 常见问题解答
Q:后台任务未执行可能的原因? A:检查项目结构确保路由正确导入,验证任务函数是否真正异步执行,检查SMTP服务配置
🛠️ 报错解决方案:SMTPAuthenticationError 535
- 检查.env文件中的账号密码是否正确
- 确认SMTP服务是否启用专用应用密码
- 验证网络连接是否允许出站SMTP请求
第二章:邮件服务集成与安全实践
2.1 SMTP协议安全配置
强制使用SSL加密连接:
python
async def send_email(...):
# 在连接配置中强制使用TLS
await aiosmtplib.send(
...,
start_tls=True,
tls_context=ssl.create_default_context()
)
2.2 邮件模板引擎集成
使用Jinja2模板示例:
python
from jinja2 import Environment, FileSystemLoader
env = Environment(loader=FileSystemLoader("templates/email"))
async def render_template(template_name: str, context: dict) -> str:
template = env.get_template(template_name)
return template.render(context)
🔍 Quiz:如何处理邮件发送失败的重试? A) 使用指数退避算法重试 B) 立即抛出异常 正确答案:A。在send_email函数中添加重试逻辑:
python
from tenacity import retry, stop_after_attempt, wait_exponential
@retry(stop=stop_after_attempt(3), wait=wait_exponential())
async def send_email(...):
# 原有发送逻辑
第三章:日志系统深度集成
3.1 结构化日志配置
python
import logging
import json
from pythonjsonlogger import jsonlogger
logger = logging.getLogger("api")
logger.setLevel(logging.INFO)
handler = logging.FileHandler("app.log")
formatter = jsonlogger.JsonFormatter(
"%(asctime)s %(levelname)s %(message)s"
)
handler.setFormatter(formatter)
logger.addHandler(handler)
3.2 请求日志中间件
python
@app.middleware("http")
async def log_requests(request: Request, call_next):
start_time = time.time()
response = await call_next(request)
process_time = (time.time() - start_time) * 1000
log_data = {
"method": request.method,
"path": request.url.path,
"status": response.status_code,
"latency": f"{process_time:.2f}ms"
}
logger.info(log_data)
return response
⚙️ 性能优化技巧:
- 使用RotatingFileHandler防止日志文件过大
- 生产环境建议接入ELK或Sentry等日志系统
- 敏感信息过滤(在中间件中添加过滤逻辑)