Vercel — AI · · 1 min read

Run background tasks with Celery on Vercel

Mirrored from Vercel — AI for archival readability. Support the source by reading on the original site.

Celery, an asynchronous distributed task queue for Python, is now supported natively on Vercel. Tasks are executed as Vercel Functions and automatically scale with traffic.

worker.py
from celery import Celery
app = Celery("celery", broker="vercel://")
@app.task
def add(x, y):
return x + y

By default, task results are stored in Runtime Cache, which is sufficient for small data sizes and relatively short workflow runtimes. For workloads that require stronger persistence guarantees, more storage or longer retention, a durable result backend can be configured.

app.py
from fastapi import FastAPI
from worker import add, app as celery
app = FastAPI()
@app.post("/add")
def enqueue(x: int, y: int):
return { "id": add.delay(x, y).id }
@app.get("/result/{task_id}")
def result(task_id):
task = celery.AsyncResult(task_id)
return { "status": task.status, "result": task.result }

Celery workers can be declared as subscribers in pyproject.toml.

pyproject.toml
[[tool.vercel.subscribers]]
entrypoint = "worker:app"

When running on Vercel, the vercel:// broker is automatically installed and to use Vercel Queues, and the default Celery broker_url is set to vercel://.

Celery workloads on Vercel use Fluid compute with Active CPU pricing by default. This means your Celery workers will automatically scale up and down based on traffic, and you only pay for what you use.

Deploy Celery workloads on Vercel or visit the Python runtime documentation.

Discussion (0)

Sign in to join the discussion. Free account, 30 seconds — email code or GitHub.

Sign in →

No comments yet. Sign in and be the first to say something.

More from Vercel — AI