The documentation explains how to send background tasks from controllers, like
@app.get("/mypath")
async def send_notification(email: str, background_tasks: BackgroundTasks):
pass
I wonder if there's a way to emit a background task out of controller scope (I mean, without needing to pass the BackgroundTasks
object from controller to over all my function calls)
I did try this but didn't work
from fastapi import BackgroundTasks
def print_key(key: str):
print(f"test bg task: {key}")
def send_stats(key: str):
BackgroundTasks().add_task(print_key, key)