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
asyncio
andasyncpg
to handle events without blocking your main application logic. - PostgreSQL Notifications: Seamlessly integrates with PostgreSQL's
LISTEN/NOTIFY
to 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 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,
"max_connections": 1,
}
@app.before_serving
async def setup():
app.task_events = asyncio.create_task(events.server(db_config))
@app.after_serving
async def cleanup():
app.task_events.cancel()
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
File details
Details for the file pg_async_events-0.1.0.tar.gz
.
File metadata
- Download URL: pg_async_events-0.1.0.tar.gz
- Upload date:
- Size: 3.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 44ac9dd6d3d7ccbb95431c5bcfe18df91f8b27da0d016a2267bfa565c6361fad |
|
MD5 | d0e74dc6511e8f8d6634a5409b2ac8d5 |
|
BLAKE2b-256 | 7839e598a20e9641ecd3f1b6b58c5b88cd60541d2f9f7ea123339a2ca75a1fc2 |
File details
Details for the file pg_async_events-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: pg_async_events-0.1.0-py3-none-any.whl
- Upload date:
- Size: 4.3 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 | 0ed0008ac986b68d2d15f876edfd5e9a952057a57ca5a414c24d77b3e1ac328b |
|
MD5 | 2aaf5377305097fe570c7445b7af37bf |
|
BLAKE2b-256 | f349b8505820b557a407c40dc01299e6c230b2febf1bdbecb8f8f31c43847c67 |