How to run tasks background tasks
Start a background task worker
To start a worker to run background tasks for a single function, define a task and call .serve()
on it.
Trigger a background task
To trigger a background task, call .delay()
on the same task in a different terminal.
.delay()
has the same signature as the @task
decorated function.
Start a worker to run background tasks for multiple functions
To start a worker to run background tasks for multiple functions, call serve()
with the tasks you want to run.
Submit multiple background tasks
To submit multiple background tasks, call .map()
on the task with deferred=True
.
.map()
accepts Iterable[P.args]
, Iterable[P.kwargs]
or unmapped
inputs.
If you want to keep one argument mapped and the other unmapped, you can do so by passing unmapped
to the mapped argument.
Further reading
- How to deploy a web application with Prefect background tasks
- Prefect Background Task Examples repository for reference implementations of applications leveraging background tasks.