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

507
Views
FastAPI backgroud task block other api request

It's seem like fastapi asynchronous background tasks blocks other requests? but it seems my problem is a bit different

from fastapi import BackgroundTasks, FastAPI

app = FastAPI()
db = Database()

async def task(data):
    otherdata = await db.fetch("some sql")
    await db.execute("insert otherdata sql",otherdata) # this blocks other api requests when excute'time too long. 

@app.post("/profile")
async def profile(data: Data, background_tasks: BackgroundTasks):
    background_tasks.add_task(task, data)
    return {}

But when not define async it will not block other api request. (https://fastapi.tiangolo.com/tutorial/background-tasks/)

from fastapi import BackgroundTasks, FastAPI
from time import sleep

app = FastAPI()
db = Database()

def task(data):
     for x in range(10):
        sleep(1)
        print(x)

@app.post("/profile")
async def profile(data: Data, background_tasks: BackgroundTasks):
    background_tasks.add_task(task, data)
    return {}

--> My prolem is When I excute sql (long execution time), i must be using async/await but using with background task of fast api, it will block other api request. So Is there any way I define my task is sync and excute sql by async/await.

over 4 years ago · Santiago Trujillo
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!