A simple async event handling library using PostgreSQL notifications.
Project description
pg-async-events
A simple asynchronous event handling library using PostgreSQL notifications. This library allows you to subscribe to and publish events using PostgreSQL's LISTEN/NOTIFY mechanism in an asynchronous environment.
This is useful in generative AI applications or for situations where other pub/sub systems are overkill.
Features
- Asynchronous Event Handling: Leverage
asyncioandasyncpgto handle events without blocking your main application logic. - PostgreSQL Notifications: Seamlessly integrates with PostgreSQL's
LISTEN/NOTIFYto provide real-time event notifications. - Simple API: Easy-to-use API for subscribing to channels and notifying events.
Installation
You can install the package directly from PyPI:
pip install pg_async_events
Usage
Quart
import asyncpg
import pg_async_events as events
# Add your postgres credentials from the env
db_config = {
"user": POSTGRES_USER,
"password": POSTGRES_PASSWORD,
"host": POSTGRES_HOST,
"port": POSTGRES_PORT,
"min_size": 1,
"max_size": 5,
}
@app.before_serving
async def setup():
pool = await asyncpg.create_pool(**db_config)
await events.initialize(pool)
Notifications
import pg_async_events as events
async def listen_to_events():
async for message in events.subscribe('your_channel'):
print('Received:', message)
async def publish_event():
payload = {'key': 'value'}
await events.notify('your_channel', payload)
Requirements
- Python 3.8+
- PostgreSQL 9.0+ (with support for LISTEN/NOTIFY)
- asyncpg library
Author
Built by 255labs.xyz, the AI product and consulting startup helping people adapt to the AI age with consulting, product, and open-source development.
Contributing
Contributions are welcome! Please submit a pull request or open an issue to discuss the changes.
License
This project is licensed under the MIT License. See the LICENSE file for more details.
Thanks
- asyncpg for providing the asynchronous PostgreSQL driver.
- PostgreSQL community for their robust database and features like LISTEN/NOTIFY.
Project details
Release history Release notifications | RSS feed
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 pg_async_events-0.1.2.tar.gz.
File metadata
- Download URL: pg_async_events-0.1.2.tar.gz
- Upload date:
- Size: 3.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
48df49cd8959ffef4559e12cc5b23579ef1fb2e2cbf0403678f3ac72bc1d1934
|
|
| MD5 |
d249dfd741a8508053f71c6eb3a96ab3
|
|
| BLAKE2b-256 |
26844b8f44f7151cab8eadf10db0194ebde58e396ae4d64bc55b6481bc43260c
|
File details
Details for the file pg_async_events-0.1.2-py3-none-any.whl.
File metadata
- Download URL: pg_async_events-0.1.2-py3-none-any.whl
- Upload date:
- Size: 4.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
498216c3d9fe813687a1ddf6c93f6a954969c72ad1ebc0d8e48c416922a954f6
|
|
| MD5 |
f1742d663951b42e75548a3df90ac615
|
|
| BLAKE2b-256 |
4b10dd29f6b97f9b90e7ed64240f710ca2a9b75bcef617de98d47a0904b530f6
|