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)
相关推荐
默_笙2 天前
🍙 给每个请求过安检:FastAPI 是怎么把校验写进类型注解的
python
qq_426003962 天前
启动playwright录制codegen生成自动化测试脚本
python·自动化
虎头金猫2 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
长沙三为智能科技2 天前
家政小程序开发从0到上线:五阶段交付流程与验收清单
python
伞伞悦读2 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
只睡四小时2 天前
Canvas 弹道联机实战:700 行 + 固定时间步长
python·websocket·html5·游戏开发·canvas
奇思妙想聪明勤奋的小羊2 天前
DeepAgents第5章:子Agent 与上下文隔离—让 Agent学会委派
人工智能·python·学习·语言模型
lpfasd1232 天前
2026年第38周GitHub趋势周报
python·科技·github
IZero072 天前
Jev 与 Laya
python·语言模型