From 8164e703c735d59718394e11b190335fac1bb558 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sat, 17 Aug 2024 08:33:46 -0500 Subject: [PATCH] Improve performance of starting request handlers with Python 3.12+ (#8681) (cherry picked from commit 30a3d0ef1ddfec7351d7dc322e738be14c29759a) --- CHANGES/8681.misc.rst | 3 +++ aiohttp/web_protocol.py | 10 ++++++++-- 2 files changed, 11 insertions(+), 2 deletions(-) create mode 100644 CHANGES/8681.misc.rst 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