Future from granian being wrongly treated #3084
Unanswered
hermeschen1116
asked this question in
Potential Issue
Replies: 2 comments
-
|
Do you have an MRE? |
Beta Was this translation helpful? Give feedback.
0 replies
-
|
Here is how I implement the API. @router.get(
"/capture/stream",
status_code=status.HTTP_200_OK,
responses={
"200": {"description": "回傳串流擷取結果"},
"404": {"description": "不存在的串流"},
"422": {"description": "錯誤的 UUID 格式"},
"503": {"description": "擷取串流時出現錯誤"},
},
description="擷取指定串流畫面",
)
async def stream_frame(
request: Request,
stream_id: typing.Annotated[
uuid.UUID, Query(description="串流來源 ID", example="07c9f116-feb1-5e24-97fc-f400c03e935a")
],
):
"""擷取串流畫面"""
if not request.app.state.streams:
await LOGGER.awarning(msg := "🏞️ 無任何串流")
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=msg)
if request.app.state.streams.get(stream_id) is None:
await LOGGER.awarning(msg := f"🏞️ 串流(stream_id:{stream_id})不存在")
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail=msg)
async def frame_streamer():
frame_queue: CyclicPriorityQueue = await request.app.state.frame_buffer.register()
try:
while not (await request.is_disconnected()):
_, frames = await frame_queue.get()
if (image := frames[stream_id].frame) is None:
continue
yield (
b"--frame\r\nContent-Type: image/webp\r\n\r\n"
+ image
+ b"\r\nX-TIMESTAMP: "
+ frames[stream_id].timestamp.isoformat().encode("utf-8")
+ b"\r\nX-STREAM-ID: "
+ str(frames[stream_id].stream_id).encode("utf-8")
+ b"\r\n"
)
await LOGGER.ainfo(f"🏞️ 擷取串流畫面({frames[stream_id].timestamp})")
except asyncio.CancelledError as e:
await LOGGER.awarning(f"⚠️ Client({request.client})中斷連接")
raise e
finally:
await request.app.state.frame_buffer.unregister(frame_queue)
return StreamingResponse(frame_streamer(), media_type="multipart/x-mixed-replace; boundary=frame") |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
@Kludex Hello, I’ve encountered some issues using sse-starlette with Granian and have reached out to the Granian maintainer for help. You can find the details of the issue on GitHub at emmett-framework/granian#698. He suspects that the future might be incorrectly handled in either FastAPI or Starlette. While this only results in a warning, would you be able to assist me with this?
Beta Was this translation helpful? Give feedback.
All reactions