summaryrefslogtreecommitdiff
path: root/docs
diff options
context:
space:
mode:
authorSam Toyer <sam@qxcv.net>2023-07-29 01:43:15 -0700
committerMariusz Felisiak <felisiak.mariusz@gmail.com>2023-09-11 19:53:21 +0200
commit64cea1e48f285ea2162c669208d95188b32bbc82 (patch)
tree490e610188dabdd2fe9b2c9e7d36d51f11002468 /docs
parenta7c73b944f51d6c92ec876fd7e0a171e7c01657d (diff)
Fixed #34752 -- Fixed handling ASGI http.disconnect for streaming responses.
Diffstat (limited to 'docs')
-rw-r--r--docs/ref/request-response.txt30
-rw-r--r--docs/topics/async.txt3
2 files changed, 33 insertions, 0 deletions
diff --git a/docs/ref/request-response.txt b/docs/ref/request-response.txt
index e70dae4de7..ee98b4b8b1 100644
--- a/docs/ref/request-response.txt
+++ b/docs/ref/request-response.txt
@@ -1282,6 +1282,36 @@ Attributes
This is useful for middleware needing to wrap
:attr:`StreamingHttpResponse.streaming_content`.
+.. _request-response-streaming-disconnect:
+
+Handling disconnects
+--------------------
+
+.. versionadded:: 5.0
+
+If the client disconnects during a streaming response, Django will cancel the
+coroutine that is handling the response. If you want to clean up resources
+manually, you can do so by catching the ``asyncio.CancelledError``::
+
+ async def streaming_response():
+ try:
+ # Do some work here
+ async for chunk in my_streaming_iterator():
+ yield chunk
+ except asyncio.CancelledError:
+ # Handle disconnect
+ ...
+ raise
+
+
+ async def my_streaming_view(request):
+ return StreamingHttpResponse(streaming_response())
+
+This example only shows how to handle client disconnection while the response
+is streaming. If you perform long-running operations in your view before
+returning the ``StreamingHttpResponse`` object, then you may also want to
+:ref:`handle disconnections in the view <async-handling-disconnect>` itself.
+
``FileResponse`` objects
========================
diff --git a/docs/topics/async.txt b/docs/topics/async.txt
index b16ffe0f78..1faf787380 100644
--- a/docs/topics/async.txt
+++ b/docs/topics/async.txt
@@ -197,6 +197,9 @@ cleanup::
# Handle disconnect
raise
+You can also :ref:`handle client disconnects in streaming responses
+<request-response-streaming-disconnect>`.
+
.. _async-safety:
Async safety