I have a resource intensive async method that i want to run as a background task. Example code for it looks like this:
@staticmethod
async def trigger_task(id: str, run_e2e: bool = False):
try:
add_status_for_task(id)
result1, result2 = await task(id)
update_status_for_task(id, result1, result2)
except Exception:
update_status_for_task(id, 'FAIL')
@router.post("/task")
async def trigger_task(background_tasks: BackgroundTasks):
background_tasks.add_task(EventsTrigger.trigger_task)
return {'msg': 'Task submitted!'}
When i trigger this endpoint, I expect an instant output: {'msg': 'Task submitted!'}
. But instead the api output is awaited till the task completes. I am following this documentation from fastapi.
fastapi: v0.70.0 python: v3.8.10
I believe the issue is similar to what is described here. Request help in making this a non-blocking call.