Body:
[`StreamingResponse` docs](https://fastapi.tiangolo.com/advanced/custom-response/?h=responses#streamingresponse) state:
> Takes an async generator or a normal generator/iterator
However based on https://github.com/Kludex/starlette/discussions/1776#discussioncomment-3207518 `async` generetors need something `await`ed to work:
```py
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`
--- Comments ---
User: casperdcl
It seems based on https://github.com/Kludex/starlette/discussions/1776#discussioncomment-3207518 that `await asyncio.sleep(0)` needs to be added to the documented example to work.
User: mirza-mohibul-hasan
Hi @casperdcl, thanks for raising this. I understand the issue and the root cause with async generators blocking without an await. If the fix isn’t already finalized, I’d be happy to work on this or help refine the docs/validate the example. Please let me know if that works for you.
User: casperdcl
fixed in #14681
Body:
[`StreamingResponse` docs](https://fastapi.tiangolo.com/advanced/custom-response/?h=responses#streamingresponse) state:
> Takes an async generator or a normal generator/iterator
However based on https://github.com/Kludex/starlette/discussions/1776#discussioncomment-3207518 `async` generetors need something `await`ed to work:
```py
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`
--- Comments ---
User: casperdcl
It seems based on https://github.com/Kludex/starlette/discussions/1776#discussioncomment-3207518 that `await asyncio.sleep(0)` needs to be added to the documented example to work.
User: mirza-mohibul-hasan
Hi @casperdcl, thanks for raising this. I understand the issue and the root cause with async generators blocking without an await. If the fix isn’t already finalized, I’d be happy to work on this or help refine the docs/validate the example. Please let me know if that works for you.
User: casperdcl
fixed in #14681
Body:
[`StreamingResponse` docs](https://fastapi.tiangolo.com/advanced/custom-response/?h=responses#streamingresponse) state:
> Takes an async generator or a normal generator/iterator
However based on https://github.com/Kludex/starlette/discussions/1776#discussioncomment-3207518 `async` generetors need something `await`ed to work:
```py
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`
--- Comments ---
User: casperdcl
It seems based on https://github.com/Kludex/starlette/discussions/1776#discussioncomment-3207518 that `await asyncio.sleep(0)` needs to be added to the documented example to work.
User: mirza-mohibul-hasan
Hi @casperdcl, thanks for raising this. I understand the issue and the root cause with async generators blocking without an await. If the fix isn’t already finalized, I’d be happy to work on this or help refine the docs/validate the example. Please let me know if that works for you.
User: casperdcl
fixed in #14681