@@ -42,6 +42,84 @@ Or using **Docker Compose** to start Postgres with ``PGMQ`` extension:
4242
4343 For more information, see `PGMQ GitHub <https://github.com/tembo-io/pgmq >`_.
4444
45- PGMQ SQLAlchemy
46- ---------------
45+ pgmq-sqlalchemy Setup
46+ ---------------------
4747
48+ .. tip ::
49+
50+ See `API Reference <api-reference >`_ for **more examples and detailed usage **.
51+
52+ For ``dispatcher.py ``:
53+
54+ .. code-block :: python
55+
56+ from typing import List
57+ from pgmq_sqlalchemy import PGMQueue
58+
59+ postgres_dsn = ' postgresql://postgres:postgres@localhost:5432/postgres'
60+
61+ pgmq = PGMQueue(dsn = postgres_dsn)
62+ pgmq.create_queue(' my_queue' )
63+
64+ msg = {' key' : ' value' , ' key2' : ' value2' }
65+ msg_id:int = pgmq.send(' my_queue' , msg)
66+
67+ # could also send a list of messages
68+ msg_ids:List[int ] = pgmq.send_batch(' my_queue' , [msg, msg])
69+
70+ .. seealso ::
71+
72+ .. _init_method : ref:`pgmq_sqlalchemy.PGMQueue.__init__`
73+ .. |init_method | replace :: :py:meth: `~pgmq_sqlalchemy.PGMQueue.__init__ `
74+
75+ .. _send_method : ref:`pgmq_sqlalchemy.PGMQueue.send`
76+ .. |send_method | replace :: :py:meth: `~pgmq_sqlalchemy.PGMQueue.send `
77+
78+ See |init_method |_ for more options on how to initialize the ``PGMQueue `` object, and advance usage with |send_method |_ on `API Reference <api-reference >`_.
79+
80+
81+ For ``consumer.py ``:
82+
83+ .. code-block :: python
84+
85+ from pgmq_sqlalchemy import PGMQueue
86+ from pgmq_sqlalchemy.schema import Message
87+
88+ postgres_dsn = ' postgresql://postgres:postgres@localhost:5432/postgres'
89+
90+ pgmq = PGMQueue(dsn = postgres_dsn)
91+
92+ # read a single message
93+ msg:Message = pgmq.read(' my_queue' )
94+
95+ # read a batch of messages
96+ msgs:List[Message] = pgmq.read_batch(' my_queue' , 10 )
97+
98+ .. seealso ::
99+
100+ .. _read_with_poll_method : ref:`pgmq_sqlalchemy.PGMQueue.read_with_poll`
101+ .. |read_with_poll_method | replace :: :py:meth: `~pgmq_sqlalchemy.PGMQueue.read_with_poll `
102+
103+ .. _read_method : ref:`pgmq_sqlalchemy.PGMQueue.read`
104+ .. |read_method | replace :: :py:meth: `~pgmq_sqlalchemy.PGMQueue.read `
105+
106+ See |read_with_poll_method |_ for reading messages with long-polling, and advance usage with |read_method |_ for **consumer retries mechanism ** and more control over message consumption on `API Reference <api-reference >`_.
107+
108+ For ``monitor.py ``:
109+
110+ .. code-block :: python
111+
112+ from pgmq_sqlalchemy import PGMQueue
113+ from pgmq_sqlalchemy.schema import QueueMetrics
114+
115+ postgres_dsn = ' postgresql://postgres:postgres@localhost:5432/postgres'
116+
117+ pgmq = PGMQueue(dsn = postgres_dsn)
118+
119+ # get queue metrics
120+ metrics:QueueMetrics = pgmq.metrics(' my_queue' )
121+ print (metrics.queue_length)
122+ print (metrics.total_messages)
123+
124+
125+
0 commit comments