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)