Fastapi之BackgroundTasks

如何在 FastAPI 中使用 BackgroundTasks 来异步执行后台任务

python 复制代码
import asyncio
import logging
import time

from fastapi import FastAPI, BackgroundTasks

# 配置日志记录级别
logging.basicConfig(level=logging.INFO)

# 初始化FastAPI应用
app = FastAPI(routes=None)

# 初始化任务状态字典
task_status = {}

def send_mail(n, task_id):
    """
    模拟发送邮件的任务函数。
    
    参数:
    - n: 邮件发送预计耗时的秒数。
    - task_id: 任务的唯一标识符。
    """
    logging.info(f"开始发送邮件,预计耗时 {n} 秒,任务ID: {task_id}")
    time.sleep(n)  # 模拟邮件发送耗时
    logging.info(f"邮件发送完成,耗时 {n} 秒,任务ID: {task_id}")
    task_status[task_id] = "completed"  # 更新任务状态为完成

@app.api_route(path="/index", methods=["GET", "POST"])
async def index(tasks: BackgroundTasks):
    """
    主要的API路由处理函数。
    
    参数:
    - tasks: 用于在后台执行任务的任务管理器。
    
    返回:
    - 一个包含任务ID的字典。
    """
    task_id = str(int(time.time() * 1000))  # 生成一个唯一的任务ID
    task_status[task_id] = "running"  # 更新任务状态为运行中
    tasks.add_task(send_mail, 10, task_id)  # 将邮件发送任务添加到后台任务中
    print(id(asyncio.get_event_loop()))  # 打印当前事件循环的ID
    return {"index": "index", "task_id": task_id}

@app.get("/status/{task_id}")
async def get_task_status(task_id: str):
    """
    获取任务状态的API路由处理函数。
    
    参数:
    - task_id: 任务的唯一标识符。
    
    返回:
    - 一个包含任务ID和状态的字典。
    """
    status = task_status.get(task_id, "not found")  # 获取任务状态,如果不存在则返回"not found"
    return {"task_id": task_id, "status": status}

if __name__ == "__main__":
    import uvicorn
    import os
    
    # 获取应用模型名称,用于uvicorn运行
    app_model_name = os.path.basename(__file__).replace(".py", "")
    print(app_model_name)
    
    # 使用uvicorn运行FastAPI应用
    uvicorn.run(f"{app_model_name}:app", host='0.0.0.0', reload=True)
相关推荐
SelectDB6 小时前
Apache Doris Python UDF:让 SQL 直接调用 Python 生态,支撑 Agent 时代复杂业务逻辑
大数据·数据库·python
荣码14 小时前
GraphRAG:普通RAG只能回答"点"的问题,我踩了4个坑才搞懂
java·python
金銀銅鐵1 天前
[Python] 基于欧几里得算法,实现分数约分计算器
python·数学
Lyn_Li1 天前
Kaggle Top 5 | 198只股票、200条数据的金融预测——BattleFin高分方案从零复现
python·kaggle·比赛复盘·金融预测
小九九的爸爸1 天前
前端想要入门Agent开发,要具备哪些Python基础?
python·agent·ai编程
阿耶同学1 天前
手把手教你用 LangGraph 搭建三层嵌套 Agent 架构
python·程序员
花酒锄作田2 天前
Pydantic校验配置文件
python
hboot2 天前
AI工程师第四课 - 深度学习入门
pytorch·python·神经网络
ZhengEnCi2 天前
P2M-Matplotlib折线图完全指南-从数据可视化到趋势分析的Python绘图利器
python·matlab·数据可视化
ZhengEnCi3 天前
P2L-Matplotlib饼图完全指南-从数据可视化到图表定制的Python绘图利器
python·matlab