Given Django 3.1's upcoming support for async views, it's worth us talking though if there are useful points of async support in REST framework, and what they would be if so.
I'm going to prefix this by starting with a bit of expectation setting... Django 3.1's async view support is a really impressive bit of foundational work, but there's currently limitations to where it's actually valuable, given that the ORM isn't yet async-capable.
One thing that'd be really helpful to this discussion would be concrete use-cases where folks demonstrate an actual use-case where they've used or would use an async view in Django, together with the motivation, and demonstrable improvements vs. sticking with a regular sync view.
We'd also want to scoping this down to the most minimal possible starting point.
In particular, what would we need to change in order to support this?...
@api_view(['GET'])
async def my_view(request):
...
The @api_view decorator is implemented on top of the APIView class based view, so an even more minimal question is: what would we need to change in order to support something like this?...
class MyView(AsyncAPIView):
async def get(self, request):
...
There's a whole bunch of different things to consider there, eg...
- The authentication/permissions is likely to be a sync ORM operation at the moment.
- The throttling is likely to be a sync cache operation at the moment.
- Does Django support reading the request body as an async operation? How would we need to tie this in with the parsing.
But let's just put those aside for the moment.
Django's upcoming docs for 3.1 async views mentions...
For a class-based view, this means making its __call__() method an async def
(not its __init__() or as_view()).
So here's some even simpler questions:
- What does a Django 3.1 async CBV look like?
- What would using REST framework's
Request explicitly within a Django async view look like (rather than wrapping with @api_view). What provisos are there on operations on the request instance that are currently sync?
- What would using REST framework's
Response explicitly within a Django async view look like (rather than wrapping with @api_view). Are there any provisos on sync operations there?
Given Django 3.1's upcoming support for async views, it's worth us talking though if there are useful points of async support in REST framework, and what they would be if so.
I'm going to prefix this by starting with a bit of expectation setting... Django 3.1's async view support is a really impressive bit of foundational work, but there's currently limitations to where it's actually valuable, given that the ORM isn't yet async-capable.
One thing that'd be really helpful to this discussion would be concrete use-cases where folks demonstrate an actual use-case where they've used or would use an async view in Django, together with the motivation, and demonstrable improvements vs. sticking with a regular sync view.
We'd also want to scoping this down to the most minimal possible starting point.
In particular, what would we need to change in order to support this?...
The
@api_viewdecorator is implemented on top of theAPIViewclass based view, so an even more minimal question is: what would we need to change in order to support something like this?...There's a whole bunch of different things to consider there, eg...
But let's just put those aside for the moment.
Django's upcoming docs for 3.1 async views mentions...
So here's some even simpler questions:
Requestexplicitly within a Django async view look like (rather than wrapping with @api_view). What provisos are there on operations on the request instance that are currently sync?Responseexplicitly within a Django async view look like (rather than wrapping with @api_view). Are there any provisos on sync operations there?