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("host=your_host port=your_port dbname=your_database user=your_username password=your_password") - Define a callback function that will be executed when a notification is received.
- Use the
subscribe_to_channelmethod to attach your callback function to the notification channel:listener.subscribe_to_channel("channel_to_listen", callback_function) - Start listening for notifications by calling the
startmethod:listener.start() - You can trigger a notification from PostgreSQL by
NOTIFY channel_to_listenstatement. - The
startmethod will call all attached callbacks once when called. If you don't want this behaviour, pass theinitial_run=Falseargument to the start method:listener.start(initial_run=False)
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_channelmethod 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=Falseas an argument. If you want to call all callback after X seconds if no notification is received, passpoll_interval=X.
- subscribe_to_channel : Attaches a callback function to a specific channel. The
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 postgresql_notification_listener-3.2.4.tar.gz.
File metadata
- Download URL: postgresql_notification_listener-3.2.4.tar.gz
- Upload date:
- Size: 4.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c4fd4a51c4cf050eb50bd84a73c7e3f933db2031759d7477da7f973f3cb6c000
|
|
| MD5 |
8f43f03aa8daae9e976a24c40ffb709e
|
|
| BLAKE2b-256 |
e1ec1a6984ec409b7299c0e9415049fbf3a38b5090e178c16889ad4d02b4e1ae
|
Provenance
The following attestation bundles were made for postgresql_notification_listener-3.2.4.tar.gz:
Publisher:
publish.yml on tvuotila/postgresql-notification-listener
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
postgresql_notification_listener-3.2.4.tar.gz -
Subject digest:
c4fd4a51c4cf050eb50bd84a73c7e3f933db2031759d7477da7f973f3cb6c000 - Sigstore transparency entry: 1247008341
- Sigstore integration time:
-
Permalink:
tvuotila/postgresql-notification-listener@51396e31124ba1c339c831d93c9cdbde963d255c -
Branch / Tag:
refs/tags/v3.2.4 - Owner: https://github.com/tvuotila
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@51396e31124ba1c339c831d93c9cdbde963d255c -
Trigger Event:
push
-
Statement type:
File details
Details for the file postgresql_notification_listener-3.2.4-py3-none-any.whl.
File metadata
- Download URL: postgresql_notification_listener-3.2.4-py3-none-any.whl
- Upload date:
- Size: 5.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
975fad542ccb704b133366b9d9234ddc8d222698a78390c8fb2c70fc65445f6f
|
|
| MD5 |
663eb0e0bc69844e6ba32edb046f38fb
|
|
| BLAKE2b-256 |
6ab9ddabb40bd038b88c76551968c4a1739512b253bf283b85aa451e1b4ba6e9
|
Provenance
The following attestation bundles were made for postgresql_notification_listener-3.2.4-py3-none-any.whl:
Publisher:
publish.yml on tvuotila/postgresql-notification-listener
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
postgresql_notification_listener-3.2.4-py3-none-any.whl -
Subject digest:
975fad542ccb704b133366b9d9234ddc8d222698a78390c8fb2c70fc65445f6f - Sigstore transparency entry: 1247008356
- Sigstore integration time:
-
Permalink:
tvuotila/postgresql-notification-listener@51396e31124ba1c339c831d93c9cdbde963d255c -
Branch / Tag:
refs/tags/v3.2.4 - Owner: https://github.com/tvuotila
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@51396e31124ba1c339c831d93c9cdbde963d255c -
Trigger Event:
push
-
Statement type: