Background Tasks

Yaat has built-in background task runner using BackgroundTask.

If you want to run a task in the background after responding to the client, you will have to wrap your Response and BackgroundTask in RunAfterResponse and return.

Background Task

Use to run a single background task after responding to a client.

BackgroundTask(function, *args, **kwargs)

  • function - function to be called from the background task. It can be either sync or async.
from yaat.background import BackgroundTask, RunAfterResponse
from yaat.responses import TextResponse

async def task():
    ...

background_task = BackgroundTask(task)

@app.route("/")
async def handler(request):
    response = TextResponse("Hello World")
    return RunAfterResponse(response, background_task)

Background Tasks

Use to run multiple background tasks after responding to a client.

from yaat.background import BackgroundTasks
from yaat.responses import RunAfterResponse, TextResponse

async def task1():
    ...

async def task2(something):
    ...

background_tasks = BackgroundTasks()
background_tasks.add(task1)
background_tasks.add(task2, "hello")

@app.route("/")
async def handler(request):
    response = TextResponse("Hello World")
    return RunAfterResponse(response, background_tasks)