diff --git a/CHANGES/8681.misc.rst b/CHANGES/8681.misc.rst new file mode 100644 index 00000000000..222787d36da --- /dev/null +++ b/CHANGES/8681.misc.rst @@ -0,0 +1,3 @@ +Improved performance of starting request handlers with Python 3.12+ -- by :user:`bdraco`. + +This change is a followup to :issue:`8661` to make the same optimization for Python 3.12+ where the request is connected. diff --git a/aiohttp/web_protocol.py b/aiohttp/web_protocol.py index 635b668ceb0..39e1c8be50e 100644 --- a/aiohttp/web_protocol.py +++ b/aiohttp/web_protocol.py @@ -292,10 +292,16 @@ def connection_made(self, transport: asyncio.BaseTransport) -> None: if self._tcp_keepalive: tcp_keepalive(real_transport) - self._task_handler = self._loop.create_task(self.start()) assert self._manager is not None self._manager.connection_made(self, real_transport) + loop = self._loop + if sys.version_info >= (3, 12): + task = asyncio.Task(self.start(), loop=loop, eager_start=True) + else: + task = loop.create_task(self.start()) + self._task_handler = task + def connection_lost(self, exc: Optional[BaseException]) -> None: if self._manager is None: return @@ -494,7 +500,7 @@ async def start(self) -> None: keep_alive(True) specified. """ loop = self._loop - handler = self._task_handler + handler = asyncio.current_task(loop) assert handler is not None manager = self._manager assert manager is not None