Skip to content

Commit 0aabe17

Browse files
committed
LISTEN/NOTIFY funcionality
Signed-off-by: chandr-andr (Kiselev Aleksandr) <chandr@chandr.net>
1 parent 9e1118e commit 0aabe17

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

python/psqlpy/_internal/__init__.pyi

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1753,7 +1753,60 @@ class ConnectionPoolBuilder:
17531753
"""
17541754

17551755
class Listener:
1756-
"""Result."""
1756+
"""Listener for LISTEN command.
1757+
1758+
Can be used two ways:
1759+
1) As a background task
1760+
2) As an asynchronous iterator
1761+
1762+
## Examples
1763+
1764+
### Background task:
1765+
1766+
```python
1767+
async def callback(
1768+
channel: str,
1769+
payload: str,
1770+
process_id: int,
1771+
connection: Connection,
1772+
) -> None: ...
1773+
async def main():
1774+
pool = ConnectionPool()
1775+
1776+
listener = pool.listener()
1777+
await listener.add_callback(
1778+
channel="test_channel",
1779+
callback=callback,
1780+
)
1781+
await listener.startup()
1782+
1783+
listener.listen()
1784+
```
1785+
1786+
### Async iterator
1787+
```python
1788+
from psqlpy import
1789+
1790+
async def msg_processor(
1791+
msg: ListenerNotificationMsg,
1792+
) -> None:
1793+
...
1794+
1795+
1796+
async def main():
1797+
pool = ConnectionPool()
1798+
1799+
listener = pool.listener()
1800+
await listener.add_callback(
1801+
channel="test_channel",
1802+
callback=callback,
1803+
)
1804+
await listener.startup()
1805+
1806+
for msg in listener:
1807+
await msg_processor(msg)
1808+
```
1809+
"""
17571810

17581811
connection: Connection
17591812

0 commit comments

Comments
 (0)