---
source: https://iinventedlight.com/library/notes/starlette-background-tasks.md
title: "Run a Starlette or FastAPI background task after the response is sent"
license: CC-BY-4.0
license_url: https://creativecommons.org/licenses/by/4.0/
source_license: "BSD-3-Clause (Starlette); MIT (FastAPI); prose restates cited source lines in own words"
license_family: mixed-green
attribution: "Summary by iinventedlight (AI-assisted). Sources: Starlette, Background Tasks; FastAPI, Background Tasks. BSD-3-Clause; MIT."
author: "iinventedlight (synthesis of cited sources); text drafted with AI assistance"
date: 2026-11-19
status: published
approved_by: "Cole Withington, 2026-11-20 13:14 PT: \"Publish the full batch and rewrite the 6 live notes\""
checked_on: 2026-11-19
as_of: 2026-11-19
confidence: medium
untested: "Not reproduced (2026-11-19). Official docs and source files were read; no test suite, server, or API call was run for this note."
category: coding-references
topic_area: "code-python: Starlette BackgroundTask and FastAPI BackgroundTasks"
tags: [starlette, fastapi, background-tasks, asgi]
client: Librarian
sources:
  - id: S1
    title: "Background Tasks"
    publisher: Starlette
    url: https://starlette.dev/background/
    accessed: 2026-11-19
    license: "BSD-3-Clause"
  - id: S2
    title: "Background Tasks"
    publisher: FastAPI
    url: https://fastapi.tiangolo.com/tutorial/background-tasks/
    accessed: 2026-11-19
    license: "MIT"
---
# Run a Starlette or FastAPI background task after the response is sent
**Answer:** Starlette's `BackgroundTask` class is for in-process background tasks. Attach it to a response; it runs after that response has been sent. In FastAPI you can define background tasks to be run after returning a response.

## Starlette BackgroundTask

The signature is `BackgroundTask(func, *args, **kwargs)`. The signup example builds a task with `send_welcome_email` and returns `JSONResponse(message, background=task)`. The example returns `JSONResponse(message, background=task)` from signup.

## FastAPI

Use this for operations that need to happen after a request, without making the client wait. Email notifications are the usual example, because connecting to an email server and sending an email tends to be slow. Processing data, such as a file that must go through a slow process, is another case: return the response right away and do that work in the background.

| Starlette | FastAPI |
|---|---|
| BackgroundTask | In-process task attached to a response |
| background=task | Passed to a response such as JSONResponse |
| after returning a response | When FastAPI runs the task |
| email notifications | Example that should not block the response |

## See also

- [fastapi-background-tasks](https://iinventedlight.com/library/notes/fastapi-background-tasks/)
- [fastapi-async-await](https://iinventedlight.com/library/notes/fastapi-async-await/)
- [django-request-response](https://iinventedlight.com/library/notes/django-request-response/)

## Claims

- id: C1
  claim: A Starlette background task is attached to a response and runs after the response has been sent.
  source: S1
  evidence: "will run only once the response has been sent"

- id: C2
  claim: BackgroundTask takes a function plus args and kwargs.
  source: S1
  evidence: "BackgroundTask(func, *args, **kwargs)"

- id: C3
  claim: FastAPI can define background tasks to be run after returning a response.
  source: S2
  evidence: "background tasks to be run after returning a response"

## Sources

- [S1] Background Tasks. Starlette. https://starlette.dev/background/ . License: BSD-3-Clause. Accessed 2026-11-19.

- [S2] Background Tasks. FastAPI. https://fastapi.tiangolo.com/tutorial/background-tasks/ . License: MIT. Accessed 2026-11-19.

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Starlette, Background Tasks; FastAPI, Background Tasks. BSD-3-Clause; MIT. Text was condensed and changed from the originals.

---

Agents: [/agents.md](/agents.md) | [/.well-known/agent-library.json](/.well-known/agent-library.json).
