From 1465bd91cbb6f99ba4854528d29c6d3b0d24f4c5 Mon Sep 17 00:00:00 2001 From: Iurii Pliner Date: Sun, 1 Sep 2024 16:54:52 +0100 Subject: [PATCH] Eagerly start notification processing with Python 3.12+ (#212) * Eagerly start notification processing with Python 3.12+ * Add to CHANGELOG.md --- CHANGELOG.md | 1 + asyncpg_listen/listener.py | 13 ++++++++----- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 661da20..a5a5997 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ * [Implement metrics/tracing on top of OpenTelemetry](https://github.com/anna-money/asyncpg-listen/pull/199) * [Support python 3.12 and end support of 3.8](https://github.com/anna-money/asyncpg-listen/pull/211) +* [Eagerly start notification processing with Python 3.12+](https://github.com/anna-money/asyncpg-listen/pull/212) ## v0.0.6 (2022-11-02) diff --git a/asyncpg_listen/listener.py b/asyncpg_listen/listener.py index 9b4d527..c1dfbe0 100644 --- a/asyncpg_listen/listener.py +++ b/asyncpg_listen/listener.py @@ -140,12 +140,15 @@ async def _process_notifications( if notification is None: continue - # to have independent async context per run - # to protect from misuse of contextvars try: - await asyncio.create_task( - self._process_notification(handler, notification), name=f"{__package__}.{channel}" - ) + # 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 except Exception: logger.exception("Failed to handle %s", notification)