Browse Source

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
pull/14991/head
Hiren 5 months ago
parent
commit
03ca680039
  1. 6
      docs/en/docs/advanced/custom-response.md

6
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] *} {* ../../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 } #### Using `StreamingResponse` with file-like objects { #using-streamingresponse-with-file-like-objects }
If you have a <a href="https://docs.python.org/3/glossary.html#term-file-like-object" class="external-link" target="_blank">file-like</a> object (e.g. the object returned by `open()`), you can create a generator function to iterate over that file-like object. If you have a <a href="https://docs.python.org/3/glossary.html#term-file-like-object" class="external-link" target="_blank">file-like</a> object (e.g. the object returned by `open()`), you can create a generator function to iterate over that file-like object.

Loading…
Cancel
Save