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

200
Visualizações
Async Await Not working when using in an endpoint to fetch result from SQL database

I am new to async await and trying to use it for getting response from a SQL db query method defined at an end point

@router.get('/queryDAS')
async def fetch_das_db():
response = await DASDBData().get_all()
logger.info("---ASYNCRONOUS----")
return (json.dumps(response))

but all I get is this error : response = await DASDBData().get_all() TypeError: object list can't be used in 'await' expression

def get_all(self):
    """Get all new data.
    @returns:
        Return list of dictionaries with team information.
    """
  
    result=[]
    statement = """SELECT *
                FROM CAE_APPR_ITEM_RESPONSE WHERE ROWNUM <= 500
                """
    logger.debug(statement)
    cur = self.connection.create_connection().cursor()
    cur.execute(statement)
    for row in cur:
        print(row)
        result.append(row)
    #return (200,result)
    return result

I thought that the message "---ASYNCHRONOUS---" would be printed first as the get_all method would go inside the event_loop but it didnt get printed as well

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

0

TL;DR

@router.get('/queryDAS')
async def fetch_das_db():
    response = DASDBData().get_all()
    logger.info("---ASYNCRONOUS----")
    return (json.dumps(await response))

Starting from the comments, I really suggest you to read first how async await works. Here is what I believe is a good tutorial about async await.

After you've read the tutorial/explanation, you will understand that response = await DASDBData().get_all() will wait for the result of get_all() before going on...behaving like the traditional synchronous code.

Now, if you remove the await keyword, your print statement will run before the method has finished, but you'll also get a warning about your coroutine not being awaited. This is similar to starting a project, but never ending it. The response variable will thus hold the coroutine, some sort of future result, which is not there yet.

With this in mind, you can then return the result while awaiting it, modifying the last return statement into return (json.dumps(await response)). This will wait for the future result and dump it as string before returning it.

If you need to use the result for some other things, you can import asyncio and then use response = await asyncio.gather(response) to get your response value. This will simply wait for the running coroutine and return the resulting value (i.e. execution is blocked until the function returns something). You can then do whatever you want with the result.

One side note:

FastAPI automatically converts the return value to JSON. Unless you are using a special formatting, I advice to simply return await response (the await may or may not be needed, based on which approach you use).

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