From 03ca680039176bbc4d03c4240d71f9816dc6f7e6 Mon Sep 17 00:00:00 2001 From: Hiren Date: Tue, 24 Feb 2026 09:58:20 -0500 Subject: [PATCH] Add warning about blocking operations in async generators for StreamingResponse Async generators need to use asyncio.sleep() instead of time.sleep() to avoid blocking the event loop and prevent proper streaming. Fixes: #14680 --- docs/en/docs/advanced/custom-response.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/docs/en/docs/advanced/custom-response.md b/docs/en/docs/advanced/custom-response.md index e88e95865..acb2acf7c 100644 --- a/docs/en/docs/advanced/custom-response.md +++ b/docs/en/docs/advanced/custom-response.md @@ -169,6 +169,12 @@ Takes an async generator or a normal generator/iterator and streams the response {* ../../docs_src/custom_response/tutorial007_py310.py hl[2,14] *} +/// warning + +When using an **async generator**, make sure to use `asyncio.sleep()` instead of `time.sleep()`. Using blocking operations like `time.sleep()` in an async generator will block the event loop and prevent proper streaming. + +/// + #### Using `StreamingResponse` with file-like objects { #using-streamingresponse-with-file-like-objects } If you have a file-like object (e.g. the object returned by `open()`), you can create a generator function to iterate over that file-like object.