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

Fix missing loop parameter for asyncio.Task #213

Merged
merged 1 commit into from
Sep 1, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading