Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

775
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda