Discussion: Async view support #7774
-
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 class MyView(AsyncAPIView):
async def get(self, request):
... 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:
|
Beta Was this translation helpful? Give feedback.
Replies: 50 comments 52 replies
-
I'm dropping those comments not because they're problematic, but because I think they're derailing the issue somewhat. I don't really want to have a conversation here about the relative merits of opting for an async-native framework. There's great options there and growing support for a wider async ecosystem which encode is also playing it's part in developing. Django is starting to gain some built-in async support. That's useful in some limited cases, and it'd be worthwhile for REST framework to track Django's async support as it grows, even if that comes with some significant caveats at this point in time. |
Beta Was this translation helpful? Give feedback.
-
Btw. maybe https://github.com/hishnash/djangochannelsrestframework can be an simple inspiration |
Beta Was this translation helpful? Give feedback.
-
Would ❤️ some kind of native, official websocket story for DRF that allows reuse of existing DRF components. This would help position DRF as an option for more real-time bi-directional use cases. |
Beta Was this translation helpful? Give feedback.
-
This one is not strictly for views, but the async view would be a precondition for this to be possible. I've been wanting for some time to be able to use async in my serializers, to speed it up when I have highly IO bound calls in my custom serializer fields. Example
For this simple example we would not gain a lot, but some times there are many such custom fields, or even some times I am serializing many cars in a nested serializer. In these cases I imagine there would be a lot to gain. |
Beta Was this translation helpful? Give feedback.
-
Here's a few more use cases that gain a benefit from varying degrees of async support:
|
Beta Was this translation helpful? Give feedback.
-
Expressing my interest to see this happen! I have seen the benchmark for fastapi and DRF, and fastapi is faster. Would love to stick to DRF as I am comfortable with Django and don't have a lot of time to learn fastapi to quickly deliver a good performing API. |
Beta Was this translation helpful? Give feedback.
-
I am also very interested in async support in Django and DRF. Our use case is pretty common I believe. We make IO network calls within our Django models or serializers to an external API. We try to perform most of those calls in Celery but given that those calls are not computationally intensive, that would be simpler to keep them within Django itself. |
Beta Was this translation helpful? Give feedback.
-
@hadim You should be able to do this already with a Django 3.1 view. Use httpx to make the network calls. You don't even need to use ASGI. Just define an async def view and run under WSGI as normal, and Django takes care of the rest. This seems to be the core use-case. It's not clear what DRF needs to add? 🤔 |
Beta Was this translation helpful? Give feedback.
-
@carltongibson I'm not sure |
Beta Was this translation helpful? Give feedback.
This comment has been hidden.
This comment has been hidden.
-
@TheBlusky ah, yes ViewSet... fair point. |
Beta Was this translation helpful? Give feedback.
-
Is there any work on this subject ? Is there any way to help on this matter ? |
Beta Was this translation helpful? Give feedback.
-
@TheBlusky you could experiment with what's required to get an APIView working here. That's the first step I think. |
Beta Was this translation helpful? Give feedback.
-
We have a use case of downloading a file from a cloud storage provider and then passing it on to the client, A good starting point would be the
|
Beta Was this translation helpful? Give feedback.
-
My use case for this is a flow whereby RPC calls are made from my Django Server to RabbitMQ and all the way back to Django and the response is sent to the client who made the Django HTTP call. These RPC messages could take some time to complete and we ideally don't want our application to be blocking while waiting for the RPC reply to come into Django. We would love to use the power of DRF alongside non-blocking async capable API endpoints and all the community-packages that work on top of DRF, such as DRF Access Policy. DRF Access Policy would allow us to control access permissions while maintaining our application as async. |
Beta Was this translation helpful? Give feedback.
-
Here's my current Use Case which would really benefit from async DRF. I have a custom user Profile which includes potentially a number of media items (image, audio) stored in S3 with django-storages. While I can use celery etc to perform the longer-running S3 write task, it adds another layer of abstraction as I have to again serialize all the necessary values to pass to the worker queue, and create a safe way of writing the results back to the DB (probably in a sync worker). It would be VERY convenient if I could just use async directly with the DRF API and not worry about all that extra abstraction. |
Beta Was this translation helpful? Give feedback.
-
Any updates on this? Any advise on how to handle this situation? Feel free to request extra details if needed |
Beta Was this translation helpful? Give feedback.
-
I/O bound tasks would benefit very much from this. You don't or can't always spin up celery (or whatever task queue system) task for these cases. |
Beta Was this translation helpful? Give feedback.
-
Hi |
Beta Was this translation helpful? Give feedback.
-
The most basic use-case I repeatedly come up against:
That's it :) |
Beta Was this translation helpful? Give feedback.
-
2022 and still no support for async? |
Beta Was this translation helpful? Give feedback.
-
Django 4.1 just released with async handlers for class based views as well as an async ORM interface. 🚀 Could DRF add support? import asyncio
from django.http import HttpResponse
from django.views import View
class AsyncView(View):
async def get(self, request, *args, **kwargs):
# Perform view logic using await.
await asyncio.sleep(1)
return HttpResponse("Hello async world!") async for author in Author.objects.filter(name__startswith="A"):
book = await author.books.afirst() |
Beta Was this translation helpful? Give feedback.
-
As a use case - our app has several routes that call external APIs (such as Okta's Python SDK), and we'd like to be able to run these requests asyncronously. Okta's API in particular is 100% asyncronous, so we end up having to wrap any Okta function calls in an async_to_sync in order to use them in our backend views. Being able to create async views would allow us to considerably speed up those third party API calls. |
Beta Was this translation helpful? Give feedback.
-
What about this approach hisdream86/drf-async-view (adding async implementations of existing view/request methods, not just using sync_to_async in one place (like in django and in this pr #8496) ? |
Beta Was this translation helpful? Give feedback.
-
Hi @tomchristie, now that Django 4.1 officially supports async class-based views, I hope you will be interested in including async class-based views is DRF as well in future updates. |
Beta Was this translation helpful? Give feedback.
-
We now have support for async views, through the Async Django REST framework package. We're in a position to accept a pull request adding this to the documentation as a third party package. |
Beta Was this translation helpful? Give feedback.
-
adrf doesn't support lots of drf parts. actually you should implement a lot of things if you start to use adrf. |
Beta Was this translation helpful? Give feedback.
-
He is right. The implementation of ADRF seems poor and appears to lack a lot of stability. We really need a more stable and well-maintained version! |
Beta Was this translation helpful? Give feedback.
-
There's no update in this discussion? The async/await recently I start a project to serve as SSO app for my many other applications and this feature is essential I start to implement the ADRF in my project to see how it's going to work. |
Beta Was this translation helpful? Give feedback.
-
does drf oficially announce for async support?? |
Beta Was this translation helpful? Give feedback.
We now have support for async views, through the Async Django REST framework package.
We're in a position to accept a pull request adding this to the documentation as a third party package.