Skip to main content

Easily LISTEN to PostgreSQL NOTIFY notifications

Project description

pgnotify: Easily LISTEN to PostgreSQL NOTIFY notifications

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:///nameofdatabase',
        ['channel1', 'channel2']):

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

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

CONNECT = "postgresql:///example"
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,
  ):
      # when n is an integer, a signal has been has been caught for further handling.
      if isinstance(n, int):
          sig = signal.Signals(n)
          if n in SIGNALS_TO_HANDLE:
              print(f"handling {sig.name}")
          print("interrupted, stopping")
          break

      # if `yield_on_timeout` has been set to True, the loop returns None after the timeout has been reached
      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.1541159130.tar.gz (3.9 kB view details)

Uploaded Source

Built Distribution

pgnotify-0.1.1541159130-py2.py3-none-any.whl (7.6 kB view details)

Uploaded Python 2 Python 3

File details

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

File metadata

  • Download URL: pgnotify-0.1.1541159130.tar.gz
  • Upload date:
  • Size: 3.9 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.1541159130.tar.gz
Algorithm Hash digest
SHA256 b5136c51801438ebf5ca20b09334a7722e9dfd9a532e591d3ab0d66555134a67
MD5 4ac1119e7362b239cc5a888817f2bb89
BLAKE2b-256 5542c2db6daa18226b47062b364fc8884b5822fb7c716d290e8a15317b16ae15

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for pgnotify-0.1.1541159130-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 1f7ba4615000f9bde93df2c05d7d2987d427703bfed4da72a2f2df2ff31eca03
MD5 5d21bfba4d51fbec4ff4ad56b3d22529
BLAKE2b-256 e55d71c535ac32dffb390f9152712cb22e03320353ce0060373b04970371dd16

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