Skip to main content

Easily LISTEN to PostgreSQL NOTIFY notifications

Project description

pgnotify: A python library to easily LISTEN to PostgreSQL NOTIFY notifications

Example

LISTEN to and process NOTIFY events with a simple for loop, like so:

from pgnotify import await_pg_notifications

for notification in await_pg_notifications(
        'postgresql:///example',
        ['channel1', 'channel2']):

    print(notification.channel)
    print(notification.payload)

Install

Installable with any python package manager from the python package index, eg:

pip install pgnotify

All the bells and whistles

You can also handle timeouts and signals, as in this more fully-fleshed example:

import signal

from pgnotify import await_pg_notifications, get_dbapi_connection

# the first parameter of the await_pg_notifications
# loop is a dbapi connection in autocommit mode
CONNECT = "postgresql:///example"

# use this convenient method to create the right connection
# from a database URL
e = get_dbapi_connection(CONNECT)

SIGNALS_TO_HANDLE = [signal.SIGINT, signal.SIGTERM]

for n in await_pg_notifications(
    e,
    ["hello", "hello2"],
    timeout=10,
    yield_on_timeout=True,
    handle_signals=SIGNALS_TO_HANDLE,
):
    # the integer code of the signal is yielded on each
    # occurrence of a handled signal
    if isinstance(n, int):
        sig = signal.Signals(n)
        if n in SIGNALS_TO_HANDLE:
            print(f"handling {sig.name}")
        print("interrupted, stopping")
        break

    # the `yield_on_timeout` option makes the
    # loop yield `None` on timeout
    elif n is None:
        print("timeout, continuing")

    # handle the actual notify occurrences here
    else:
        print((n.pid, n.channel, n.payload))

Further documentation to come.

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

pgnotify-0.1.1541160523.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

pgnotify-0.1.1541160523-py2.py3-none-any.whl (7.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file pgnotify-0.1.1541160523.tar.gz.

File metadata

  • Download URL: pgnotify-0.1.1541160523.tar.gz
  • Upload date:
  • Size: 4.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/0.12.5 CPython/3.7.0 Linux/4.4.0-137-generic

File hashes

Hashes for pgnotify-0.1.1541160523.tar.gz
Algorithm Hash digest
SHA256 80b8c84c4fe51dc9609ea09fafd3b97ffbd8632b449d8e3ad28441fa1ecfdd12
MD5 d47e1264a7ff3d0e459165222cf90a1d
BLAKE2b-256 b6803865ca70dd908306a0b19a386ccc1008e3165e43d26549d5b294eade3fbd

See more details on using hashes here.

File details

Details for the file pgnotify-0.1.1541160523-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for pgnotify-0.1.1541160523-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 f93bad832a582640b8e25695e2c723553a73f6bcbc810c34e0f7ebbf69cc4e42
MD5 c62969b56e27512d346b53844fbbb0b3
BLAKE2b-256 c35db310be8ce4c9fab9b7ee2c6201a832a6738694e470052c50ac04d7d24a78

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