Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move Config#load out of Server#serve to enable Eager Asynchronous Construction of App #942

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
9 changes: 6 additions & 3 deletions uvicorn/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,16 +44,19 @@ 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()
Comment on lines +47 to +51
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add a comment about why we're doing this here, perhaps?

Suggested change
config = self.config
config.setup_event_loop()
if not config.loaded:
config.load()
# Setup and load app before entering the async environment, so it's possible to
# run any async setup on our side or in the app module.
# See: https://github.com/encode/uvicorn/issues/941
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))

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)

Expand Down
7 changes: 6 additions & 1 deletion uvicorn/workers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down