Opened 2 years ago

Closed 2 years ago

#33611 closed New feature (fixed)

Allow View subclasses to define async method handlers.

Reported by: Carlton Gibson Owned by: Carlton Gibson
Component: Generic views Version: 4.0
Severity: Normal Keywords: async, asgi, views
Cc: Andrew Godwin Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Carlton Gibson)

The current 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!")

Change History (8)

comment:1 by Carlton Gibson, 2 years ago

Cc: Andrew Godwin added

comment:2 by Carlton Gibson, 2 years ago

Component: HTTP handlingGeneric views

comment:3 by Carlton Gibson, 2 years ago

Description: modified (diff)

comment:4 by Mariusz Felisiak, 2 years ago

Triage Stage: UnreviewedAccepted

comment:5 by Mariusz Felisiak, 2 years ago

Owner: changed from nobody to Carlton Gibson

comment:6 by Mariusz Felisiak, 2 years ago

Patch needs improvement: set

comment:7 by Mariusz Felisiak, 2 years ago

Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

comment:8 by GitHub <noreply@…>, 2 years ago

Resolution: fixed
Status: assignedclosed

In 9ffd4ea:

Fixed #33611 -- Allowed View subclasses to define async method handlers.

Note: See TracTickets for help on using tickets.
Back to Top