docs/examples/onboarding/flask/streaming.py
raw| 1 | from collections.abc import AsyncIterator |
| 2 | |
| 3 | from litestar import Litestar, get |
| 4 | from litestar.response import Stream |
| 5 | |
| 6 | |
| 7 | async def event_source() -> AsyncIterator[bytes]: |
| 8 | for i in range(3): |
| 9 | yield f"data: {i}\n\n".encode() |
| 10 | |
| 11 | |
| 12 | @get("/stream") |
| 13 | async def stream() -> Stream: |
| 14 | return Stream(event_source(), media_type="text/event-stream") |
| 15 | |
| 16 | |
| 17 | app = Litestar(route_handlers=[stream]) |