From 1d1ddffc27cd55c011298cd09bfa4de3fa73cf7a Mon Sep 17 00:00:00 2001 From: th3nn3ss Date: Wed, 21 Dec 2022 14:25:24 -0500 Subject: Fixed #33738 -- Allowed handling ASGI http.disconnect in long-lived requests. --- docs/releases/5.0.txt | 7 +++++++ docs/topics/async.txt | 20 ++++++++++++++++++++ 2 files changed, 27 insertions(+) (limited to 'docs') diff --git a/docs/releases/5.0.txt b/docs/releases/5.0.txt index 6e911f471e..cc4fb69ee3 100644 --- a/docs/releases/5.0.txt +++ b/docs/releases/5.0.txt @@ -192,6 +192,13 @@ Minor features * ... +Asynchronous views +~~~~~~~~~~~~~~~~~~ + +* Under ASGI, ``http.disconnect`` events are now handled. This allows views to + perform any necessary cleanup if a client disconnects before the response is + generated. See :ref:`async-handling-disconnect` for more details. + Cache ~~~~~ diff --git a/docs/topics/async.txt b/docs/topics/async.txt index 95d3435e07..5a2324af5e 100644 --- a/docs/topics/async.txt +++ b/docs/topics/async.txt @@ -136,6 +136,26 @@ a purely synchronous codebase under ASGI because the request-handling code is still all running asynchronously. In general you will only want to enable ASGI mode if you have asynchronous code in your project. +.. _async-handling-disconnect: + +Handling disconnects +-------------------- + +.. versionadded:: 5.0 + +For long-lived requests, a client may disconnect before the view returns a +response. In this case, an ``asyncio.CancelledError`` will be raised in the +view. You can catch this error and handle it if you need to perform any +cleanup:: + + async def my_view(request): + try: + # Do some work + ... + except asyncio.CancelledError: + # Handle disconnect + raise + .. _async-safety: Async safety -- cgit v1.3