Skip to main content

Execute functions on PostgreSQL notifications

Project description

PostgreSQL Notification Listener

This project is a Python library that listens to notifications from a PostgreSQL database. It provides a simple way to execute functions when specific events happen in the database.

How it works

The listener connects to the PostgreSQL database and sets up a notification channel. You can then attach callbacks to this channel, which will be executed whenever a notification is received. The listener tries to minimize the times callbacks are called. When there are multiple pending notifications on the same channel, the callback is called only once. This means that the notification payload and PID are ignored.

Installation

To install the library, run: pip install postgresql-notification-listener

Usage

To use this library, follow these steps:

  • Import the library in your Python script: from postgresql_notification_listener import NotificationListener
  • Create instance of the listener listener = NotificationListener("postgresql://localhost/postgres")
  • Define a callback function that will be executed when a notification is received.
  • Use the subscribe_to_channel method to attach your callback function to the notification channel: listener.subscribe_to_channel("channel_to_listen", callback_function)
  • You must register all callback functions before calling the start method.
  • You cannot unregister callback functions after calling the start method.
  • Start listening for notifications by calling the start method: listener.start()
  • You can trigger a notification from PostgreSQL by NOTIFY channel_to_listen statement.
  • The start method will call all attached callbacks once when called. If you don't want this behaviour, pass the initial_run=False argument to the start method: listener.start(initial_run=False)
  • NotificationListener can be used as a context manager: with NotificationListener("connection string") as listener:. The database connection automatically closes when exiting the with block.

API

  • NotificationListener: The main class of this library. It is responsible for setting up a notification channel and managing callbacks.
    • subscribe_to_channel : Attaches a callback function to a specific channel. The subscribe_to_channel method takes two required parameters: the name of the channel to listen to and the callback function to execute when a notification is received.
    • start : Starts listening for notifications. If you don't want all attached callbacks to be called once when called, pass initial_run=False as an argument. If you want to call all callback after X seconds if no notification is received, pass poll_interval=X.

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

postgresql_notification_listener-3.2.5.tar.gz (4.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file postgresql_notification_listener-3.2.5.tar.gz.

File metadata

File hashes

Hashes for postgresql_notification_listener-3.2.5.tar.gz
Algorithm Hash digest
SHA256 28a6786be6ef529ea6b4af9d10d1d5316f35ed28d042f9fa6646b7560d3429d5
MD5 2c36c649f9d124747897b86e9d84a079
BLAKE2b-256 babadac91f46b95f637b4c7541333dc8808575168812a0dc82e8048ac5c23773

See more details on using hashes here.

Provenance

The following attestation bundles were made for postgresql_notification_listener-3.2.5.tar.gz:

Publisher: publish.yml on tvuotila/postgresql-notification-listener

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file postgresql_notification_listener-3.2.5-py3-none-any.whl.

File metadata

File hashes

Hashes for postgresql_notification_listener-3.2.5-py3-none-any.whl
Algorithm Hash digest
SHA256 9bda92545a350017d8e5af720a40fe99f77aa29e0fff63d397264604824f0a26
MD5 46d91a1f8ec70418ce15728e950f0192
BLAKE2b-256 d01cd3a2d8d33010ade2612b3fa1fe3a9cf4fad0e276e8e7f03c263900255f62

See more details on using hashes here.

Provenance

The following attestation bundles were made for postgresql_notification_listener-3.2.5-py3-none-any.whl:

Publisher: publish.yml on tvuotila/postgresql-notification-listener

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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