I am currently developing an API that has several endpoints. One of them is to register data in a database, other endpoints are related to simple CRUD endpoints (get data by id, get all data, delete data, etc).
When the register data endpoint is called, a response is almost immediately given back to the API and then a background task is started, where we fetch the data, unzip it if necessary, etc.
We are using FastAPI and async functions for this. What I have noticed, though, is that the API gets blocked by the execution of the background task. This is especially bad when I am uploading a large file to S3 in one go (not in chunks, for which i use async functions), I have to wait for the end of the upload of the full file before another request can get a response (like when requesting the get all data endpoint)).
I am a nube in parallelism and concurrency, but I was expecting the background task not the block the API.
Any ideas on how I could run this long running background task in a way that it won't block new requests to the API? Would celery be best for this?
Mock example:
@app.post("register endpoint")
async def register_data(datainput, backgroundtasks: BackgroundTasks):
#Do something
backgroundtasks.add_task(background_operation)
return JSONResponse(
status_code=200, content="Doing stuff")
)
async def background_operation():
#Doing stuff here
await function_that_uploads_data_to_s3