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

642
Visualizações
Python - What is the right way to run functions in asyncio?

I am using FastApi and have one endpoint.

I have two long running functions which I want to run concurrently using asyncio

Therefore, I have created two functions:

async def get_data_one():
    return 'done_one'

async def get_data_two():
    return 'done_two'

These functions get data from external webservices.

I want to execute them concurrently so I have created a another function that does it:

async def get_data():
    loop = asyncio.get_event_loop()
    asyncio.set_event_loop(loop)
    task_1 = loop.create_task(get_data_one)
    task_2 = loop.create_task(get_data_two)
    tasks = (task_1, task_2)
    first, second = loop.run_until_complete(asyncio.gather(*tasks))
    loop.close()

    # I will then perform cpu intensive computation on the results
    # for now - assume i am just concatenating the results
    return first + second

Finally, I have my endpoint:

@app.post("/result")
async def get_result_async():
    r = await get_data()

    return r

Even this simple example breaks and I get the following exception when I hit the endpoint:

RuntimeError: This event loop is already running ERROR: _GatheringFuture exception was never retrieved future: <_GatheringFuture finished exception=AttributeError("'function' object has no attribute 'send'",)> AttributeError: 'function' object has no attribute 'send'

This is a simplified code but I would really appreciate how to do it the right way.

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

0

When in FastAPI context, you never need to run an asyncio loop; it's always running for as long as your server process lives.

Therefore, all you need is

import asyncio


async def get_data_one():
    return "done_one"


async def get_data_two():
    return "done_two"


async def get_data():
    a, b = await asyncio.gather(get_data_one(), get_data_two())
    return a + b


#@route decorator here...
async def get_result_async():
    r = await get_data()
    print("Get_data said:", r)
    return r


# (this is approximately what is done under the hood,
#  presented here to make this a self-contained example)
asyncio.run(get_result_async())
over 4 years ago · Santiago Trujillo Relatório

0

It's as simple as:

async def get_data():
    first, second = await asyncio.gather(
        get_data_one(),
        get_data_two(),
    )

    return first + second
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