py-pg-notify is a Python library that simplifies listening to and sending notifications using PostgreSQL's `LISTEN/NOTIFY` functionality. This package leverages `asyncpg` for asynchronous communication with the database, making it ideal for real-time applications
Project description
py-pg-notify
py-pg-notify is a Python library that simplifies listening to and sending notifications using PostgreSQL's LISTEN/NOTIFY functionality. This package leverages asyncpg for asynchronous communication with the database, making it ideal for real-time applications.
📖 Features
- 🔊 PostgreSQL Notifications: Easy-to-use interfaces for
LISTEN/NOTIFY. - 🔄 Asynchronous: Fully asynchronous with support for multiple listeners and channels.
- 📦 Lightweight: Built on top of
asyncpg, offering high performance. - ⚙️ Custom Handlers: Define your notification handling logic dynamically.
🚀 Installation
Install the package via pip:
pip install py-pg-notify
Usage
Notifier Example
import asyncio
from py_pg_notify import Notifier
async def create_trigger_example():
# Define the Notifier class for PostgreSQL
notifier = Notifier(
user="<username>",
password="<password>",
host="<host>",
port=5432,
dbname="<dbname>",
)
# Connect to the database
async with notifier:
# Create a trigger function for a channel
await notifier.create_trigger_function("notify_function", "ch_01")
# Create a trigger on a specific table that calls the function
await notifier.create_trigger(
table_name="my_table",
trigger_name="my_trigger",
function_name="notify_function",
event="INSERT",
)
# Retrieve and print existing trigger functions for a table
trigger_functions = await notifier.get_trigger_functions("my_table")
print("Existing Trigger Functions:", trigger_functions)
# Remove a trigger function
await notifier.remove_trigger_function("notify_function")
# Remove the trigger from the table
await notifier.remove_trigger("my_table", "my_trigger")
if __name__ == "__main__":
asyncio.run(create_trigger_example())
Listener Example
import asyncio
from py_pg_notify import Listener
async def notification_handler(connection, pid, channel, payload):
# Perform any processing on the received notification
print(f"Notification received: Channel={channel}, Payload={payload}")
async def main():
listener = Listener(
user="<username>",
password="<password>",
host="<host>",
port=5432,
dbname="<dbname>",
)
async with listener:
await listener.add_listener("ch_01", notification_handler)
await asyncio.sleep(3600) # Simulate long-running process
if __name__ == "__main__":
asyncio.run(main())
Complete Example (Work In-Progress)
Refer to the examples/ folder for complete usage scenarios.
🧪 Testing
Run the test suite using pytest:
pip install -r requirements_test.txt
pytest --asyncio-mode=auto
📄 License
This project is licensed under the MIT License. See the LICENSE file for details.
🤝 Contributing
We welcome contributions! Please follow these steps:
- Fork the repository.
- Create a new branch.
- Make your changes.
- Add the test cases if needed.
- Test all the test cases for verfication.
- Submit a pull request.
📢 Feedback and Support
For bugs or feature requests, create an issue in the GitHub repository. We'd love to hear your feedback!
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file py_pg_notify-0.1.0.tar.gz.
File metadata
- Download URL: py_pg_notify-0.1.0.tar.gz
- Upload date:
- Size: 12.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6d96521dbff8e208cf2686b8bc9f20347d04f45942d21ba720e250daf3fbac59
|
|
| MD5 |
fd9ea71d2808a7871180a41acae55c45
|
|
| BLAKE2b-256 |
93b297719b2f390ae1add39e02c880928ddde8447cfb4b5aedacfb7424ce6843
|
File details
Details for the file py_pg_notify-0.1.0-py3-none-any.whl.
File metadata
- Download URL: py_pg_notify-0.1.0-py3-none-any.whl
- Upload date:
- Size: 13.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.0.1 CPython/3.11.0
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
78cc3f0a085d1529e7f39fc6835b2d2ad51f6577db0f5275f05c6b078a134e49
|
|
| MD5 |
a058a7f7f1f174766b9208f13cbaa708
|
|
| BLAKE2b-256 |
94d069531352d080d12bb647653856a0f3f1b719de1e1447ef91ebe4a60f9b91
|