Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

773
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!