Skip to main content

Helps to use PostgreSQL listen/notify with asyncpg

Project description

asyncpg-listen

This library simplifies usage of listen/notify with asyncpg:

  1. Handles loss of a connection
  2. Simplifies notifications processing from multiple channels
  3. Setups a timeout for receiving a notification
  4. Allows to receive all notifications/only last notification depending on ListenPolicy.
import asyncio
import asyncpg
import asyncpg_listen


async def handle_notifications(notification: asyncpg_listen.NotificationOrTimeout) -> None:
    print(f"{notification} has been received")


async def main():
    listener = asyncpg_listen.NotificationListener(asyncpg_listen.connect_func(user="postgres"))
    listener_task = asyncio.create_task(
        listener.run(
            {"simple": handle_notifications},
            policy=asyncpg_listen.ListenPolicy.LAST,
            notification_timeout=5
        )
    )

    await asyncio.sleep(1)

    connection = await asyncpg.connect(user="postgres")
    try:
        for i in range(42):
            await connection.execute(f"NOTIFY simple, '{i}'")
    finally:
        await connection.close()

    await asyncio.sleep(1)

    listener_task.cancel()


asyncio.run(main())

v0.0.6 (2022-11-02)

v0.0.5 (2022-05-23)

v0.0.4 (2022-01-24)

v0.0.3 (2022-01-23)

v0.0.2 (2021-11-02)

v0.0.1 (2021-10-27)

  • A first version

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

asyncpg-listen-0.0.6.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

asyncpg_listen-0.0.6-py3-none-any.whl (5.6 kB view details)

Uploaded Python 3

File details

Details for the file asyncpg-listen-0.0.6.tar.gz.

File metadata

  • Download URL: asyncpg-listen-0.0.6.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.11.0

File hashes

Hashes for asyncpg-listen-0.0.6.tar.gz
Algorithm Hash digest
SHA256 f3a889a3848903e4710705e785fd95940b3a5939a89a660827cdaaa113e1959e
MD5 b0a9bc0ba51beff6ee7ad76e8a619db8
BLAKE2b-256 3b1fa6f040117f88b32cffa25b3fa5984822e0b6a8c2baa854bbe5913499370d

See more details on using hashes here.

File details

Details for the file asyncpg_listen-0.0.6-py3-none-any.whl.

File metadata

File hashes

Hashes for asyncpg_listen-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 9fe21be4399f4c511a3f9a9b064306b13183487524a02f14166b33afe17ba401
MD5 1cfb40989d5bd459b7639d54cebaabff
BLAKE2b-256 8bbc98735c8d7c71ae5cd86b92676138cdd7397b1db147d45c376436578e11fe

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