Skip to content

Commit

Permalink
feat: use eager start to callback task on Python 3.12+ (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Jan 24, 2025
1 parent 1791bdb commit ab912d5
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/aioharmony/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import asyncio
import logging
import sys
from functools import partial
from typing import Optional

Expand Down Expand Up @@ -112,9 +113,17 @@ async def wrapped():
return wrapped

partial_func = async_partial(callback, result)
task = asyncio.create_task(partial_func())
_CALLBACK_TASKS.add(task)
task.add_done_callback(_CALLBACK_TASKS.discard)
loop = asyncio.get_running_loop()
if sys.version_info >= (3, 12):
# Optimization for Python 3.12, try to write
# bytes immediately to avoid having to schedule
# the task on the event loop.
task = asyncio.Task(partial_func(), loop=loop, eager_start=True)
else:
task = loop.create_task(partial_func())
if not task.done():
_CALLBACK_TASKS.add(task)
task.add_done_callback(_CALLBACK_TASKS.discard)
return True

if callable(callback):
Expand Down

0 comments on commit ab912d5

Please sign in to comment.