Skip to content

StreamingResponse doc example needs correction #14680

@casperdcl

Description

@casperdcl

StreamingResponse docs state:

Takes an async generator or a normal generator/iterator

However based on Kludex/starlette#1776 (comment) async generetors need something awaited to work:

import asyncio
from time import sleep
from fastapi import FastAPI
from fastapi.responses import StreamingResponse

app = FastAPI()

async def fake_video_streamer():
    for i in range(10):
        sleep(0.1)
        yield b"some fake video bytes"
        # await asyncio.sleep(0)  # <-- uncomment to fix example

@app.get("/")
async def main():
    """
    this is as per
    https://fastapi.tiangolo.com/advanced/custom-response/?h=responses#streamingresponse
    but does not to work (blocks for 1 sec, then returns all chunks at once):
      curl -sNo- localhost:8000
      wget -qO- localhost:8000
    """
    return StreamingResponse(fake_video_streamer())
  • uvicorn==0.40.0
  • fastapi==0.128.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions