Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

295
Visualizações
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?

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

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.

over 4 years ago · Santiago Trujillo Relatório

0

I think you can Use this decorator in DRF

import asyncio
from functools import wraps


def to_async(blocking):
    @wraps(blocking)
    def run_wrapper(*args, **kwargs):
        return asyncio.run(blocking(*args, **kwargs))

    return run_wrapper

Example of usage

@to_async
@api_view(["GET"])
async def sample_view(request):
    ...
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda