diff --git a/CHANGES/8661.misc.rst b/CHANGES/8661.misc.rst new file mode 100644 index 00000000000..c0a6fdadb37 --- /dev/null +++ b/CHANGES/8661.misc.rst @@ -0,0 +1 @@ +Improved performance of starting request handlers with Python 3.12+ -- by :user:`bdraco`. diff --git a/aiohttp/web_protocol.py b/aiohttp/web_protocol.py index 4d50465b2ef..79708a58cfb 100644 --- a/aiohttp/web_protocol.py +++ b/aiohttp/web_protocol.py @@ -1,6 +1,7 @@ import asyncio import asyncio.streams import dataclasses +import sys import traceback from collections import deque from contextlib import suppress @@ -543,9 +544,11 @@ async def start(self) -> None: request = self._request_factory(message, payload, self, writer, handler) try: # a new task is used for copy context vars (#3406) - task = self._loop.create_task( - self._handle_request(request, start, request_handler) - ) + coro = self._handle_request(request, start, request_handler) + if sys.version_info >= (3, 12): + task = asyncio.Task(coro, loop=loop, eager_start=True) + else: + task = loop.create_task(coro) try: resp, reset = await task except (asyncio.CancelledError, ConnectionError):