From f61984eb7fce31dd93d244717700fcb56e0e76b3 Mon Sep 17 00:00:00 2001 From: Mario Ishac Date: Thu, 21 Jan 2021 18:07:32 -0800 Subject: [PATCH] Move `Config#load` out of `Server#serve` --- tests/utils.py | 4 ++++ uvicorn/server.py | 9 ++++++--- uvicorn/workers.py | 7 ++++++- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/tests/utils.py b/tests/utils.py index 32ade817a..86bb4ee97 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -11,6 +11,10 @@ @asynccontextmanager async def run_server(config: Config, sockets=None): server = Server(config=config) + + if not config.loaded: + config.load() + cancel_handle = asyncio.ensure_future(server.serve(sockets=sockets)) await asyncio.sleep(0.1) try: diff --git a/uvicorn/server.py b/uvicorn/server.py index b54be77e8..3c11bf2c9 100644 --- a/uvicorn/server.py +++ b/uvicorn/server.py @@ -44,7 +44,12 @@ def __init__(self, config): self.last_notified = 0 def run(self, sockets=None): - self.config.setup_event_loop() + config = self.config + config.setup_event_loop() + + if not config.loaded: + config.load() + loop = asyncio.get_event_loop() loop.run_until_complete(self.serve(sockets=sockets)) @@ -52,8 +57,6 @@ async def serve(self, sockets=None): process_id = os.getpid() config = self.config - if not config.loaded: - config.load() self.lifespan = config.lifespan_class(config) diff --git a/uvicorn/workers.py b/uvicorn/workers.py index 7c160dff6..33aa051fd 100644 --- a/uvicorn/workers.py +++ b/uvicorn/workers.py @@ -70,7 +70,12 @@ def init_signals(self): signal.signal(s, signal.SIG_DFL) def run(self): - self.config.app = self.wsgi + config = self.config + config.app = self.wsgi + + if not config.loaded: + config.load() + server = Server(config=self.config) loop = asyncio.get_event_loop() loop.run_until_complete(server.serve(sockets=self.sockets))