X Tutup
The Wayback Machine - https://web.archive.org/web/20201011071301/https://github.com/MagicStack/asyncpg/issues/567
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Question] Asynchronous NOTIFY listener #567

Open
tomkp75 opened this issue May 5, 2020 · 3 comments
Open

[Question] Asynchronous NOTIFY listener #567

tomkp75 opened this issue May 5, 2020 · 3 comments

Comments

@tomkp75
Copy link

@tomkp75 tomkp75 commented May 5, 2020

Hello,

How would one use an async function as the callback of conn.add_listener? I would like to relay specific notifications to API calls using aiohttp.ClientSession.

Thanks,

Tom

@tomkp75 tomkp75 changed the title Asynchronous NOTIFY listener [Question] Asynchronous NOTIFY listener May 5, 2020
@elprans
Copy link
Member

@elprans elprans commented May 5, 2020

This should do the trick:

conn.add_listener(
    'CHANNEL', 
    functools.partial(asyncio.ensure_future, async_callback()),
)
@tomkp75
Copy link
Author

@tomkp75 tomkp75 commented May 5, 2020

Thanks. Would it be an idea for add_listener to take in an awaitable directly?

@charterchap
Copy link

@charterchap charterchap commented May 14, 2020

I ended up with something like this:

async def listener(self):
    async def process(*args):
        print(args)
        # await self.q.put(args)

    if not self.conn_listen:
        self.conn_listen = await asyncpg.connect(dsn)

    await self.conn_listen.add_listener('my_notify_channel',
        lambda *args: loop.create_task(process(args))
    )

def main():
    listener_task=loop.create_task(listener())
    loop.run_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.
X Tutup