2023-06-19 18:38:59 +08:00

26 lines
657 B
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import asyncio
import json
from fastapi import APIRouter
from fastapi.responses import StreamingResponse
from app.helper.progress import ProgressHelper
router = APIRouter()
@router.get("/progress/{process_type}", summary="实时进度")
async def get_progress(process_type: str):
"""
实时获取处理进度返回格式为SSE
"""
progress = ProgressHelper()
async def event_generator():
while True:
detail = progress.get(process_type)
yield 'data: %s\n\n' % json.dumps(detail)
await asyncio.sleep(0.2)
return StreamingResponse(event_generator(), media_type="text/event-stream")