Skip to content

Commit 01992d8

Browse files
Copilotjason810496
andcommitted
Simplify consumer code and add pyproject.toml for example
Co-authored-by: jason810496 <[email protected]>
1 parent 8c40f42 commit 01992d8

3 files changed

Lines changed: 27 additions & 5 deletions

File tree

examples/fastapi_pub_sub/README.md

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,14 @@ docker run -d --name postgres -e POSTGRES_PASSWORD=postgres -p 5432:5432 quay.io
3030

3131
## Installation
3232

33-
Install required dependencies using uv:
33+
Install required dependencies using uv with the example's pyproject.toml:
34+
35+
```bash
36+
cd examples/fastapi_pub_sub
37+
uv pip install -e .
38+
```
39+
40+
Or install dependencies directly:
3441

3542
```bash
3643
uv pip install fastapi uvicorn psycopg2-binary asyncpg pgmq-sqlalchemy

examples/fastapi_pub_sub/consumer.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,7 @@ async def consume_messages(pgmq: PGMQueue, batch_size: int = 10, vt: int = 30):
6767
while True:
6868
try:
6969
# Read a batch of messages using pgmq instance method
70-
async with pgmq.session_maker() as session:
71-
messages = await pgmq.read_batch_async(QUEUE_NAME, vt=vt, batch_size=batch_size, session=session, commit=True)
70+
messages = await pgmq.read_batch(QUEUE_NAME, vt=vt, batch_size=batch_size)
7271

7372
if not messages:
7473
logger.debug("No messages available, waiting...")
@@ -89,8 +88,7 @@ async def consume_messages(pgmq: PGMQueue, batch_size: int = 10, vt: int = 30):
8988
# Delete successfully processed messages using pgmq instance method
9089
for (msg_id, _), result in zip(tasks, results):
9190
if isinstance(result, bool) and result:
92-
async with pgmq.session_maker() as session:
93-
deleted = await pgmq.delete_async(QUEUE_NAME, msg_id, session=session, commit=True)
91+
deleted = await pgmq.delete(QUEUE_NAME, msg_id)
9492
if deleted:
9593
logger.info(f"Deleted message {msg_id}")
9694
elif isinstance(result, Exception):
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
[project]
2+
name = "fastapi-pub-sub-example"
3+
version = "0.1.0"
4+
description = "FastAPI pub/sub example using pgmq-sqlalchemy"
5+
readme = "README.md"
6+
requires-python = ">=3.9"
7+
dependencies = [
8+
"fastapi>=0.104.0",
9+
"uvicorn>=0.24.0",
10+
"psycopg2-binary>=2.9.9",
11+
"asyncpg>=0.29.0",
12+
"pgmq-sqlalchemy>=0.1.2",
13+
]
14+
15+
[build-system]
16+
requires = ["hatchling"]
17+
build-backend = "hatchling.build"

0 commit comments

Comments
 (0)