Yu(Irene) Chen
03/31/2026, 6:10 AMMarvin
03/31/2026, 6:10 AMMarvin
03/31/2026, 6:11 AMtask.delay(...), and a Prefect Task Worker picks it up and executes it in the background.
Why it’s useful
- Don’t block your app: Return HTTP responses immediately while work runs in the background.
- Fire-and-forget: Perfect for emails, notifications, webhooks, ETL kicks, etc.
- Decouple and scale: Run workers separately from your app and scale them independently.
- Simpler than a full queue stack: Acts like a lightweight alternative to Celery/RQ for many use cases.
How it works (quick example)
1) Define a task
from prefect import task
@task
def process_order(order_id: str):
# do work...
return f"processed {order_id}"
2) Run a Task Worker (in a separate process/container)
from prefect.task_worker import serve
if __name__ == "__main__":
serve(process_order) # worker will execute background tasks for this function
3) Submit background work from anywhere (web app, script, another service)
# returns immediately with a PrefectDistributedFuture
fut = process_order.delay("ORD-123")
# optionally, wait for the result later (in the same or different process)
result = fut.result() # blocks until done
Key points
- API: Use my_task.delay(...) (alias of apply_async(...)). There’s no prefect.background_task context manager.
- Return type: delay() gives a PrefectDistributedFuture you can poll or resolve later.
- Execution: Requires a running Task Worker (prefect.task_worker.serve(...)) connected to your Prefect server/Cloud.
- When to use: Web endpoints, event handlers, async pipelines, and any “queue this work and move on” pattern.
Docs and examples
- Concepts: Background tasks (concepts)
- How-to: Run background tasks
- Example app: Background tasks example app
If you can share your use case (e.g., FastAPI endpoint, emails, data processing), I can tailor a minimal example to match it.