From f10968be6016f35bb22fbb602fba9eac0f551028 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Fri, 27 Feb 2026 21:35:27 +0100 Subject: [PATCH] =?UTF-8?q?=F0=9F=93=9D=20Update=20docs=20source=20example?= =?UTF-8?q?=20for=20streaming=20data?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs_src/stream_data/tutorial002_py310.py | 26 ++++++++++++++++------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/docs_src/stream_data/tutorial002_py310.py b/docs_src/stream_data/tutorial002_py310.py index 7fc884fa25..aa8bcee3a9 100644 --- a/docs_src/stream_data/tutorial002_py310.py +++ b/docs_src/stream_data/tutorial002_py310.py @@ -22,23 +22,33 @@ class PNGStreamingResponse(StreamingResponse): @app.get("/image/stream", response_class=PNGStreamingResponse) async def stream_image() -> AsyncIterable[bytes]: - for chunk in read_image(): - yield chunk + with read_image() as image_file: + for chunk in image_file: + yield chunk @app.get("/image/stream-no-async", response_class=PNGStreamingResponse) def stream_image_no_async() -> Iterable[bytes]: - for chunk in read_image(): - yield chunk + with read_image() as image_file: + for chunk in image_file: + yield chunk + + +@app.get("/image/stream-no-async-yield-from", response_class=PNGStreamingResponse) +def stream_image_no_async_yield_from() -> Iterable[bytes]: + with read_image() as image_file: + yield from image_file @app.get("/image/stream-no-annotation", response_class=PNGStreamingResponse) async def stream_image_no_annotation(): - for chunk in read_image(): - yield chunk + with read_image() as image_file: + for chunk in image_file: + yield chunk @app.get("/image/stream-no-async-no-annotation", response_class=PNGStreamingResponse) def stream_image_no_async_no_annotation(): - for chunk in read_image(): - yield chunk + with read_image() as image_file: + for chunk in image_file: + yield chunk