@@ -25,6 +25,7 @@ More flexible [PGMQ Postgres extension](https://github.com/tembo-io/pgmq) Python
2525## Features
2626
2727- Supports ** async** and ** sync** ` engines ` and ` sessionmakers ` , or built from ` dsn ` .
28+ - ** Automatically** creates ` pgmq ` (or ` pg_partman ` ) extension on the database if not exists.
2829- Supports ** all postgres DBAPIs supported by sqlalchemy** .
2930 > e.g. ` psycopg ` , ` psycopg2 ` , ` asyncpg ` .. <br >
3031 > See [ SQLAlchemy Postgresql Dialects] ( https://docs.sqlalhttps://docs.sqlalchemy.org/en/20/dialects/postgresql.html )
@@ -42,6 +43,7 @@ Install with additional DBAPIs packages:
4243``` bash
4344pip install pgmq-sqlalchemy[psycopg2]
4445pip install pgmq-sqlalchemy[asyncpg]
46+ # pip install pgmq-sqlalchemy[postgres-python-driver]
4547```
4648
4749## Getting Started
@@ -61,33 +63,58 @@ docker run -d --name postgres -e POSTGRES_PASSWORD=postgres -p 5432:5432 quay.io
6163> Check [ pgmq-sqlalchemy Document] ( https://pgmq-sqlalchemy-python.readthedocs.io/en/latest/ ) for more examples and detailed usage.
6264
6365
64- ` dispatcher.py ` :
66+ For ` dispatcher.py ` :
6567``` python
68+ from typing import List
6669from pgmq_sqlalchemy import PGMQueue
6770
68- pgmq = PGMQueue(dsn = ' postgresql+psycopg://postgres:postgres@localhost:5432/postgres' )
71+ postgres_dsn = ' postgresql://postgres:postgres@localhost:5432/postgres'
72+
73+ pgmq = PGMQueue(dsn = postgres_dsn)
6974pgmq.create_queue(' my_queue' )
7075
71- pgmq.send(' my_queue' , {' key' : ' value' })
76+ msg = {' key' : ' value' , ' key2' : ' value2' }
77+ msg_id:int = pgmq.send(' my_queue' , msg)
78+
79+ # could also send a list of messages
80+ msg_ids:List[int ] = pgmq.send_batch(' my_queue' , [msg, msg])
7281```
7382
74- ` consumer.py ` :
83+ For ` consumer.py ` :
7584``` python
7685from pgmq_sqlalchemy import PGMQueue
7786from pgmq_sqlalchemy.schema import Message
7887
79- pgmq = PGMQueue(dsn = ' postgresql+psycopg://postgres:postgres@localhost:5432/postgres' )
88+ postgres_dsn = ' postgresql://postgres:postgres@localhost:5432/postgres'
89+
90+ pgmq = PGMQueue(dsn = postgres_dsn)
91+
92+ # read a single message
8093msg:Message = pgmq.read(' my_queue' )
8194
82- if msg:
83- print (msg.msg_id)
84- print (msg.message)
95+ # read a batch of messages
96+ msgs:List[Message] = pgmq.read_batch(' my_queue' , 10 )
97+ ```
98+
99+ For ` monitor.py ` :
100+ ``` python
101+ from pgmq_sqlalchemy import PGMQueue
102+ from pgmq_sqlalchemy.schema import QueueMetrics
103+
104+ postgres_dsn = ' postgresql://postgres:postgres@localhost:5432/postgres'
105+
106+ pgmq = PGMQueue(dsn = postgres_dsn)
107+
108+ # get queue metrics
109+ metrics:QueueMetrics = pgmq.metrics(' my_queue' )
110+ print (metrics.queue_length)
111+ print (metrics.total_messages)
85112```
86113
87114## Issue/ Contributing / Development
88115
89116Welcome to open an issue or pull request ! <br >
90- See [ ` Development ` on Online Document] ( https://pgmq-sqlalchemy-python.readthedocs.io/en/latest/ ) or [ CONTRIBUTING.md] ( .github/CONTRIBUTING.md ) for more information.
117+ See [ ` Development ` on Online Document] ( https://pgmq-sqlalchemy-python.readthedocs.io/en/latest/ ) or [ CONTRIBUTING.md] ( .github/CONTRIBUTING.md ) for more information.
91118
92119## TODO
93120
0 commit comments