Skip to content

AsyncConnection.notifies() gernerator blocks AsyncConnection.execute() #340

@saschathiergart

Description

@saschathiergart

I have a use case where a single AsyncConnection needs to listen on different channels. Which to listen on becomes known in runtime, e.g. connected clients determine what is listened and unlistened on. Unfortunately, when using the AsynConnection.notifies() generator, further AsyncConnection.execute() calls will hang.

async def test_notifies():
    con = await psycopg.AsyncConnection.connect(PSYCOPG_DSN, autocommit=True)
    await con.set_autocommit(True)

    async def notifier():
        i = 0
        while True: 
            await con.execute(f'notify foo, \'{i}\'')
            print(f"notified {i}")
            await asyncio.sleep(1)
            i += 1
            if i==10:
                break
        await con.close()
        
    async def receiver():
        print("receiver: LISTENING ")
        cur = con.cursor()
        await cur.execute("listen foo")
        gen = con.notifies()
        async for n in gen:
            print("receiver: notified ")
            print(n)

    workers = [notifier(), receiver()]
    await asyncio.gather(*workers)
    
asyncio.run(test_notifies())

Output of the program:

receiver: LISTENING 
notified 0

After that the program hangs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions