From 1358460aaed8ea1df5d6c67ad28b67964e92759b Mon Sep 17 00:00:00 2001 From: Iurii Pliner Date: Sun, 1 Sep 2024 23:23:04 +0100 Subject: [PATCH] Fix missing loop parameter for asyncio.Task --- asyncpg_listen/listener.py | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/asyncpg_listen/listener.py b/asyncpg_listen/listener.py index c1dfbe0..2478043 100644 --- a/asyncpg_listen/listener.py +++ b/asyncpg_listen/listener.py @@ -119,6 +119,17 @@ async def _process_notifications( policy: ListenPolicy, notification_timeout: float, ) -> None: + if sys.version_info >= (3, 12): + loop = asyncio.get_running_loop() + + async def run_coro(c: Coroutine) -> None: + await asyncio.Task(c, loop=loop, eager_start=True, name=f"{__package__}.{channel}") + + else: + + async def run_coro(c: Coroutine) -> None: + await asyncio.create_task(c, name=f"{__package__}.{channel}") + while True: notification: NotificationOrTimeout | None = None @@ -143,12 +154,7 @@ async def _process_notifications( try: # to have independent async context per run # to protect from misuse of contextvars - coro = self._process_notification(handler, notification) - if sys.version_info >= (3, 12): - task = asyncio.Task(coro, eager_start=True, name=f"{__package__}.{channel}") - else: - task = asyncio.create_task(coro, name=f"{__package__}.{channel}") - await task + await run_coro(self._process_notification(handler, notification)) except Exception: logger.exception("Failed to handle %s", notification)