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

776
Visualizações
How to send a response only after all background tasks are completed in FastAPI

Using python 3x on a Mac. I have a FastAPI server with one endpoint. In this endpoint, I want to be able to run some background tasks. After they are completed, make some computation and only then sends an HTTP response.

What I have is the following:

import asyncio

import uvicorn
from fastapi import BackgroundTasks, FastAPI

app = FastAPI()


async def log(message: str):
    print(f"start to process message: {message}")
    if message == "aaaaa":
        await asyncio.sleep(10)
    else:
        await asyncio.sleep(3)
    print(f"processed {message}")


async def process():
    tasks = [log('aaaaa'),
             log('bbbbb')]
    print("start processing the tasks")
    await asyncio.gather(*tasks)
    print(" we are done with all the tasks")


@app.post("/upload-to-process")
async def upload(background_tasks: BackgroundTasks):
    background_tasks.add_task(process)

    return {"result": "all tasks are completed successfully successfully"}


if __name__ == "__main__":
    uvicorn.run(app, host="localhost", port=8001)

what the above does is send the result immediately and not wait for the tasks to be complete.

How do I change the above code to wait till all of the backgrounds are completed and only then send the response back?

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

0

It doesn't really make sense to wait for background tasks, as they are ran by definition on the background.

If what you want is to run the function that run those tasks synchronously, i.e. not in the background, you can do this:

@app.post("/upload-to-process")
async def upload(background_tasks: BackgroundTasks):
    await process()
    return {"result": "all tasks are completed successfully successfully"}

Note, however, that if those tasks take too long (this depends on your server configuration request time limits, which usually vary between 30 seconds and 15 min), the request will time out.

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