@ -45,6 +45,60 @@ Inside of your *path operation function*, pass your task function to the *backgr
* Any sequence of arguments that should be passed to the task function in order (`email`).
* Any sequence of arguments that should be passed to the task function in order (`email`).
* Any keyword arguments that should be passed to the task function (`message="some notification"`).
* Any keyword arguments that should be passed to the task function (`message="some notification"`).
## Important: Exception Handling
Background tasks run **after** the response is sent. If an exception occurs in a background task, it won't affect the response (which already succeeded), and **the exception will not be propagated to the request handler and may go unnoticed unless explicitly handled (e.g. logging, retries, alerts)**.
This is expected behavior, but it is easy to overlook when building production systems.
- [arq](https://arq-docs.helpmanual.io/) - Async task queue for Python
- [Dramatiq](https://dramatiq.io/) - Fast and reliable task processing
## Dependency Injection { #dependency-injection }
## Dependency Injection { #dependency-injection }
Using `BackgroundTasks` also works with the dependency injection system, you can declare a parameter of type `BackgroundTasks` at multiple levels: in a *path operation function*, in a dependency (dependable), in a sub-dependency, etc.
Using `BackgroundTasks` also works with the dependency injection system, you can declare a parameter of type `BackgroundTasks` at multiple levels: in a *path operation function*, in a dependency (dependable), in a sub-dependency, etc.