Skip to content

Commit

Permalink
Create dns resolution task eagerly on python 3.12+ (#9343)
Browse files Browse the repository at this point in the history
  • Loading branch information
bdraco authored Sep 29, 2024
1 parent 8a97e03 commit 5762ed6
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGES/9342.misc.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improved performance of resolving hosts with Python 3.12+ -- by :user:`bdraco`.
17 changes: 12 additions & 5 deletions aiohttp/connector.py
Original file line number Diff line number Diff line change
Expand Up @@ -911,11 +911,18 @@ async def _resolve_host(
# the underlying lookup or else the cancel event will get broadcast to
# all the waiters across all connections.
#
resolved_host_task = asyncio.create_task(
self._resolve_host_with_throttle(key, host, port, traces)
)
self._resolve_host_tasks.add(resolved_host_task)
resolved_host_task.add_done_callback(self._resolve_host_tasks.discard)
coro = self._resolve_host_with_throttle(key, host, port, traces)
loop = asyncio.get_running_loop()
if sys.version_info >= (3, 12):
# Optimization for Python 3.12, try to send immediately
resolved_host_task = asyncio.Task(coro, loop=loop, eager_start=True)
else:
resolved_host_task = loop.create_task(coro)

if not resolved_host_task.done():
self._resolve_host_tasks.add(resolved_host_task)
resolved_host_task.add_done_callback(self._resolve_host_tasks.discard)

try:
return await asyncio.shield(resolved_host_task)
except asyncio.CancelledError:
Expand Down

0 comments on commit 5762ed6

Please sign in to comment.