Skip to content

Commit

Permalink
[PR #9343/5762ed61 backport][3.10] Create dns resolution task eagerly…
Browse files Browse the repository at this point in the history
… on python 3.12+ (#9345)

Co-authored-by: J. Nick Koston <nick@koston.org>
  • Loading branch information
patchback[bot] and bdraco authored Sep 29, 2024
1 parent 523c4ea commit 23ab28e
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 @@ -929,11 +929,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 23ab28e

Please sign in to comment.