asyncpg icon indicating copy to clipboard operation
asyncpg copied to clipboard

Feature Request: better support for LISTEN/NOTIFY?

Open racinette opened this issue 2 years ago • 3 comments

Currently, you have to execute a dummy query like SELECT 1 every time you want to get a hold of new notifications from the server. Can anything similar to aiopg be implemented in asyncpg?

https://aiopg.readthedocs.io/en/stable/examples.html#usage-of-listen-notify-commands https://www.psycopg.org/docs/connection.html#connection.poll

racinette avatar Oct 11 '23 13:10 racinette

Is this still a problem? I just tried this small test and it seems to work fine (prints "Got notification" every second). I don't need to do any dummy queries.

import asyncpg
import asyncio

async def notify(conn):
    count = 0
    while True:
        await asyncio.sleep(1)
        await conn.execute(f"notify foo, '{count}'")
        count += 1

async def notified(conn, pid, channel, payload):
    print(f"Got notification on {channel}: {payload}")

async def main():
    conn = await asyncpg.connect("postgresql://postgres:postgres@localhost")
    await conn.add_listener("foo", notified)
    await notify(conn)

asyncio.run(main())

Victor-N-Suadicani avatar Apr 24 '24 08:04 Victor-N-Suadicani