Skip to content

Commit 9c36846

Browse files
authored
[python] update queue subscribers example to use vercel.queue (#1557)
### Description Updates Python queue subscribers example to use `vercel.queue` instead of `vercel.workers` ### Demo URL <!-- Provide a URL to a live deployment where we can test your PR. If a demo isn't possible feel free to omit this section. --> ### Type of Change - [ ] New Example - [x] Example updates (Bug fixes, new features, etc.) - [ ] Other (changes to the codebase, but not to examples) ### New Example Checklist - [ ] 🛫 `npm run new-example` was used to create the example - [ ] 📚 The template wasn't used but I carefuly read the [Adding a new example](https://github.com/vercel/examples#adding-a-new-example) steps and implemented them in the example - [ ] 📱 Is it responsive? Are mobile and tablets considered?
1 parent 78a911e commit 9c36846

6 files changed

Lines changed: 18 additions & 214 deletions

File tree

‎python/queue-subscribers/README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ https://python-queue-subscribers.labs.vercel.dev/
1010

1111
## How it Works
1212

13-
The FastAPI app publishes addition and multiplication tasks with `AsyncQueueClient`. The subscriber configuration in `pyproject.toml` points to the queue client in `worker.py`, while the decorated handlers in `tasks.py` process messages from the `add` and `multiply` topics. Results are shared through a local SQLite file during development and Vercel Runtime Cache when deployed. The dashboard polls for each task until its result is available.
13+
The FastAPI app publishes addition and multiplication tasks with `vercel.queue.send`. The subscriber configuration in `pyproject.toml` points to `tasks.py`, where decorated handlers process messages from the `add` and `multiply` topics. Results are shared through a local SQLite file during development and Vercel Runtime Cache when deployed. The dashboard polls for each task until its result is available.
1414

1515
## Running Locally
1616

‎python/queue-subscribers/main.py‎

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@
55
from fastapi import FastAPI, HTTPException
66
from fastapi.responses import HTMLResponse
77
from pydantic import BaseModel
8+
from vercel.queue import send
89

910
from store import result_store
10-
from worker import queue
1111

1212
logger = logging.getLogger("queue-subscribers")
1313
logger.setLevel(logging.INFO)
@@ -291,7 +291,7 @@ async def create_task(
291291
},
292292
)
293293
try:
294-
result = await queue.send(
294+
message_id = await send(
295295
operation,
296296
{
297297
"task_id": task_id,
@@ -305,15 +305,22 @@ async def create_task(
305305
task_id,
306306
operation,
307307
)
308-
await result_store.delete(task_id)
308+
try:
309+
await result_store.delete(task_id)
310+
except Exception:
311+
logger.exception(
312+
"Failed to clean up pending task task_id=%s operation=%s",
313+
task_id,
314+
operation,
315+
)
309316
raise
310317
logger.info(
311318
"Task enqueued task_id=%s operation=%s message_id=%s",
312319
task_id,
313320
operation,
314-
result["messageId"],
321+
message_id,
315322
)
316-
return {"task_id": task_id, "message_id": result["messageId"]}
323+
return {"task_id": task_id, "message_id": message_id}
317324

318325

319326
@app.post("/tasks/add", status_code=202)

‎python/queue-subscribers/pyproject.toml‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ requires-python = ">=3.12"
66
dependencies = [
77
"fastapi",
88
"vercel-cache",
9-
"vercel-workers",
9+
"vercel-queue",
1010
]
1111

1212
[tool.uv]
1313
package = false
1414

1515
[[tool.vercel.subscribers]]
16-
entrypoint = "worker:queue"
17-
topics = ["add", "multiply"]
16+
entrypoint = "tasks"

‎python/queue-subscribers/tasks.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import logging
22
from typing import Any
33

4-
from vercel.workers import subscribe
4+
from vercel.queue import subscribe
55

66
from store import result_store
77

0 commit comments

Comments
 (0)