From dd3c6a1dba87d70850aa05093432b4dd8acbcfa7 Mon Sep 17 00:00:00 2001 From: Max Fischer Date: Wed, 18 Jan 2023 11:57:19 +0100 Subject: [PATCH] split serve to reduce diff --- uvicorn/server.py | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/uvicorn/server.py b/uvicorn/server.py index c724e22cd..61573dd32 100644 --- a/uvicorn/server.py +++ b/uvicorn/server.py @@ -66,6 +66,10 @@ def run(self, sockets: Optional[List[socket.socket]] = None) -> None: pass async def serve(self, sockets: Optional[List[socket.socket]] = None) -> None: + with self._capture_exit_signals(): + await self._serve(sockets) + + async def _serve(self, sockets: Optional[List[socket.socket]] = None) -> None: process_id = os.getpid() config = self.config @@ -74,24 +78,23 @@ async def serve(self, sockets: Optional[List[socket.socket]] = None) -> None: self.lifespan = config.lifespan_class(config) - with self._capture_exit_signals(): - message = "Started server process [%d]" - color_message = ( - "Started server process [" + click.style("%d", fg="cyan") + "]" - ) - logger.info(message, process_id, extra={"color_message": color_message}) + message = "Started server process [%d]" + color_message = ( + "Started server process [" + click.style("%d", fg="cyan") + "]" + ) + logger.info(message, process_id, extra={"color_message": color_message}) - await self.startup(sockets=sockets) - if self.should_exit: - return - await self.main_loop() - await self.shutdown(sockets=sockets) + await self.startup(sockets=sockets) + if self.should_exit: + return + await self.main_loop() + await self.shutdown(sockets=sockets) - message = "Finished server process [%d]" - color_message = ( - "Finished server process [" + click.style("%d", fg="cyan") + "]" - ) - logger.info(message, process_id, extra={"color_message": color_message}) + message = "Finished server process [%d]" + color_message = ( + "Finished server process [" + click.style("%d", fg="cyan") + "]" + ) + logger.info(message, process_id, extra={"color_message": color_message}) async def startup(self, sockets: list = None) -> None: await self.lifespan.startup()