Browse Source

fix: use await asyncio.sleep() in StreamingResponse async generator example

The async generator in the StreamingResponse docs example did not await
anything, causing all chunks to be returned at once instead of streaming.
Added await asyncio.sleep(0) so the event loop can yield between chunks,
enabling proper streaming behavior.

Closes #14680
pull/14887/head
bBlazewavE 5 months ago
parent
commit
9a3d2470eb
  1. 3
      docs_src/custom_response/tutorial007_py39.py

3
docs_src/custom_response/tutorial007_py39.py

@ -1,3 +1,5 @@
import asyncio
from fastapi import FastAPI from fastapi import FastAPI
from fastapi.responses import StreamingResponse from fastapi.responses import StreamingResponse
@ -6,6 +8,7 @@ app = FastAPI()
async def fake_video_streamer(): async def fake_video_streamer():
for i in range(10): for i in range(10):
await asyncio.sleep(0)
yield b"some fake video bytes" yield b"some fake video bytes"

Loading…
Cancel
Save