Skip to main content

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 and asyncpg 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pg_async_events-0.1.0.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

pg_async_events-0.1.0-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

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

Hashes for pg_async_events-0.1.0.tar.gz
Algorithm Hash digest
SHA256 44ac9dd6d3d7ccbb95431c5bcfe18df91f8b27da0d016a2267bfa565c6361fad
MD5 d0e74dc6511e8f8d6634a5409b2ac8d5
BLAKE2b-256 7839e598a20e9641ecd3f1b6b58c5b88cd60541d2f9f7ea123339a2ca75a1fc2

See more details on using hashes here.

File details

Details for the file pg_async_events-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pg_async_events-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0ed0008ac986b68d2d15f876edfd5e9a952057a57ca5a414c24d77b3e1ac328b
MD5 2aaf5377305097fe570c7445b7af37bf
BLAKE2b-256 f349b8505820b557a407c40dc01299e6c230b2febf1bdbecb8f8f31c43847c67

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page