Since Django now supports async views, I'm trying to change my code base which contains a lot of function based views to be async but for some reason its not working.
@api_view(["GET"])
async def test_async_view(request):
...
data = await get_data()
return Response(data)
When I send a request to this endpoint, I get an error saying:
AssertionError: Expected a
Response
,HttpResponse
orHttpStreamingResponse
to be returned from the view, but received a<class 'coroutine'>
Does DRF not support async views yet? Is there an alternative I can do to get this working?
Django has support for writing asynchronous (“async”) views, along with an entirely async-enabled request stack if you are running under ASGI. Async views will still work under WSGI, but with performance penalties, and without the ability to have efficient long-running requests.
database_sync_to_async is a version of asgiref. sync. sync_to_async that also cleans up database connections on exit. To use it, write your ORM queries in a separate function or method, and then call it with database_sync_to_async like so: from channels.db import database_sync_to_async async def connect(self): self.
An async view function in Django is detected by the annotation async def , which then runs the async view in a thread within its own event loop. This gives the benefit of being able to do and run tasks concurrently inside the async views.
An async method runs synchronously until it reaches its first await expression, at which point the method is suspended until the awaited task is complete. In the meantime, control returns to the caller of the method, as the example in the next section shows.
As of now, DRF doesn't support async "api views". Here is an open issue (#7260) in the DRF community and it is still in the discussion stage. But, Django providing a decorator/wrapper which allow us to convert our sync views/function to async using sync_to_async (...) wrapper.
Writing asynchronous code gives you the ability to speed up your application with little effort. Django versions >= 3.1 support async views, middleware, and tests. If you haven't already experimented with async views, now's a great time to get them under your belt. This tutorial looks at how to get started with Django's asynchronous views.
If you have blocking functionality in an async view, at best it's going to be no better than just using a synchronous view. If you need to make a synchronous call inside an async view (like to interact with the database via the Django ORM, for example), use sync_to_async either as a wrapper or a decorator.
Function-based Views. Django views facilitate processing the HTTP requests and providing HTTP responses. On receiving an HTTP request, Django creates an HttpRequest instance, and it is passed as the first argument to the view function. This instance contains HTTP verbs such as GET, POST, PUT, PATCH, or DELETE.
As of now, DRF doesn't support async "api views". Here is an open issue (#7260) in the DRF community and it is still in the discussion stage.
But, Django providing a decorator/wrapper which allow us to convert our sync views/function to async using sync_to_async(...)
wrapper.
Example,
@sync_to_async
@api_view(["GET"])
def sample_view(request):
data = get_data()
return Response(data)
Note that, here, sample_view(...)
and get_data(...)
are sync functions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With