[Django] #33611: Allow View subclasses to define async method handlers.

36 views
Skip to first unread message

Django

unread,
Mar 31, 2022, 3:01:50 AM3/31/22
to django-...@googlegroups.com
#33611: Allow View subclasses to define async method handlers.
-------------------------------------+-------------------------------------
Reporter: Carlton | Owner: nobody
Gibson |
Type: New | Status: assigned
feature |
Component: HTTP | Version: 4.0
handling |
Severity: Normal | Keywords: async, asgi, views
Triage Stage: | Has patch: 1
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
The current [https://docs.djangoproject.com/en/4.0/topics/async/#async-
views topic docs for Async views] say this about class-based views:

> For a class-based view, this means making its __call__() method an async
def (not its __init__() or as_view()).

This isn't really appropriate for Django's class-based views:

* We don't implement `__call__()`, rather going via `as_view() — for a
per-request instance — and then `dispatch()`.
* Users expect to implement the HTTP method handlers — `get`, `post`, and
so on — rather than these ''more internal'' bits.

Ideally we'd allow specifying `async def` at the method handler level, to
allow using `await` in the handler, and writing views such as this:

{{{
import asyncio
from django.http import HttpResponse
from django.views import View

class AsyncView(View):
async def get(self, request, *args, **kwargs):
# Perform io-blocking view-logic using await, sleep for
example.
await asyncio.sleep(1)
return HttpResponse("Hello async world!")
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/33611>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 31, 2022, 3:04:43 AM3/31/22
to django-...@googlegroups.com
#33611: Allow View subclasses to define async method handlers.
------------------------------------+--------------------------------------
Reporter: Carlton Gibson | Owner: nobody
Type: New feature | Status: assigned
Component: HTTP handling | Version: 4.0
Severity: Normal | Resolution:
Keywords: async, asgi, views | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------------+--------------------------------------
Changes (by Carlton Gibson):

* cc: Andrew Godwin (added)


Comment:

[https://github.com/django/django/pull/15559 PR]

--
Ticket URL: <https://code.djangoproject.com/ticket/33611#comment:1>

Django

unread,
Mar 31, 2022, 3:09:35 AM3/31/22
to django-...@googlegroups.com
#33611: Allow View subclasses to define async method handlers.
------------------------------------+--------------------------------------
Reporter: Carlton Gibson | Owner: nobody
Type: New feature | Status: assigned
Component: Generic views | Version: 4.0

Severity: Normal | Resolution:
Keywords: async, asgi, views | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------------+--------------------------------------
Changes (by Carlton Gibson):

* component: HTTP handling => Generic views


--
Ticket URL: <https://code.djangoproject.com/ticket/33611#comment:2>

Django

unread,
Mar 31, 2022, 3:10:40 AM3/31/22
to django-...@googlegroups.com
#33611: Allow View subclasses to define async method handlers.
------------------------------------+--------------------------------------
Reporter: Carlton Gibson | Owner: nobody
Type: New feature | Status: assigned
Component: Generic views | Version: 4.0
Severity: Normal | Resolution:
Keywords: async, asgi, views | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------------+--------------------------------------
Description changed by Carlton Gibson:

Old description:

> The current [https://docs.djangoproject.com/en/4.0/topics/async/#async-
> views topic docs for Async views] say this about class-based views:
>
> > For a class-based view, this means making its __call__() method an
> async def (not its __init__() or as_view()).
>
> This isn't really appropriate for Django's class-based views:
>
> * We don't implement `__call__()`, rather going via `as_view() — for a
> per-request instance — and then `dispatch()`.
> * Users expect to implement the HTTP method handlers — `get`, `post`, and
> so on — rather than these ''more internal'' bits.
>
> Ideally we'd allow specifying `async def` at the method handler level, to
> allow using `await` in the handler, and writing views such as this:
>
> {{{
> import asyncio
> from django.http import HttpResponse
> from django.views import View
>
> class AsyncView(View):
> async def get(self, request, *args, **kwargs):
> # Perform io-blocking view-logic using await, sleep for
> example.
> await asyncio.sleep(1)
> return HttpResponse("Hello async world!")
> }}}

New description:

The current [https://docs.djangoproject.com/en/4.0/topics/async/#async-
views topic docs for Async views] say this about class-based views:

> For a class-based view, this means making its `__call__()` method an

async def (not its `__init__()` or `as_view()`).

This isn't really appropriate for Django's class-based views:

* We don't implement `__call__()`, rather going via `as_view()` — for a
per-request instance — and then `dispatch()`.

* Users expect to implement the HTTP method handlers — `get()`, `post()`,


and so on — rather than these ''more internal'' bits.

Ideally we'd allow specifying `async def` at the method handler level, to
allow using `await` in the handler, and writing views such as this:

{{{
import asyncio
from django.http import HttpResponse
from django.views import View

class AsyncView(View):
async def get(self, request, *args, **kwargs):
# Perform io-blocking view-logic using await, sleep for
example.
await asyncio.sleep(1)
return HttpResponse("Hello async world!")
}}}

--

--
Ticket URL: <https://code.djangoproject.com/ticket/33611#comment:3>

Django

unread,
Mar 31, 2022, 3:25:20 AM3/31/22
to django-...@googlegroups.com
#33611: Allow View subclasses to define async method handlers.
------------------------------------+------------------------------------
Reporter: Carlton Gibson | Owner: nobody
Type: New feature | Status: assigned
Component: Generic views | Version: 4.0
Severity: Normal | Resolution:
Keywords: async, asgi, views | Triage Stage: Accepted

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------------+------------------------------------
Changes (by Mariusz Felisiak):

* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/33611#comment:4>

Django

unread,
Mar 31, 2022, 3:32:55 AM3/31/22
to django-...@googlegroups.com
#33611: Allow View subclasses to define async method handlers.
-------------------------------------+-------------------------------------
Reporter: Carlton Gibson | Owner: Carlton
| Gibson

Type: New feature | Status: assigned
Component: Generic views | Version: 4.0
Severity: Normal | Resolution:
Keywords: async, asgi, views | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* owner: nobody => Carlton Gibson


--
Ticket URL: <https://code.djangoproject.com/ticket/33611#comment:5>

Django

unread,
Apr 5, 2022, 2:34:33 AM4/5/22
to django-...@googlegroups.com
#33611: Allow View subclasses to define async method handlers.
-------------------------------------+-------------------------------------
Reporter: Carlton Gibson | Owner: Carlton
| Gibson
Type: New feature | Status: assigned
Component: Generic views | Version: 4.0
Severity: Normal | Resolution:
Keywords: async, asgi, views | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* needs_better_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/33611#comment:6>

Django

unread,
Apr 6, 2022, 4:46:23 AM4/6/22
to django-...@googlegroups.com
#33611: Allow View subclasses to define async method handlers.
-------------------------------------+-------------------------------------
Reporter: Carlton Gibson | Owner: Carlton
| Gibson
Type: New feature | Status: assigned
Component: Generic views | Version: 4.0
Severity: Normal | Resolution:
Keywords: async, asgi, views | Triage Stage: Ready for
| checkin

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* needs_better_patch: 1 => 0
* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/33611#comment:7>

Django

unread,
Apr 7, 2022, 1:06:22 AM4/7/22
to django-...@googlegroups.com
#33611: Allow View subclasses to define async method handlers.
-------------------------------------+-------------------------------------
Reporter: Carlton Gibson | Owner: Carlton
| Gibson
Type: New feature | Status: closed

Component: Generic views | Version: 4.0
Severity: Normal | Resolution: fixed

Keywords: async, asgi, views | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by GitHub <noreply@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"9ffd4eae2ce7a7100c98f681e2b6ab818df384a4" 9ffd4ea]:
{{{
#!CommitTicketReference repository=""
revision="9ffd4eae2ce7a7100c98f681e2b6ab818df384a4"
Fixed #33611 -- Allowed View subclasses to define async method handlers.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/33611#comment:8>

Reply all
Reply to author
Forward
0 new messages