Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use async function based views in DRF?

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 or HttpStreamingResponse 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?

like image 220
ninesalt Avatar asked Jan 24 '21 16:01

ninesalt


People also ask

What are async views in Django?

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.

What is Sync_to_async?

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.

What is asynchronous task in Django?

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.

What is the use of async method?

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.

Does DRF support async views/function?

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.

What are async views in Django?

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.

Should I use sync_to_async in my async view?

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.

What is a function based view in Django?

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.


1 Answers

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.

like image 70
JPG Avatar answered Sep 30 '22 00:09

JPG