Skip to content

Commit

Permalink
Fix missing loop parameter for asyncio.Task (#213)
Browse files Browse the repository at this point in the history
  • Loading branch information
Pliner authored Sep 1, 2024
1 parent a9587fa commit 07b5299
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions asyncpg_listen/listener.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)

Expand Down

0 comments on commit 07b5299

Please sign in to comment.