Skip to content

Instantly share code, notes, and snippets.

@RobertoPrevato
Created October 10, 2019 12:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RobertoPrevato/e504dce6e12361b108a858ccc02172ea to your computer and use it in GitHub Desktop.
Save RobertoPrevato/e504dce6e12361b108a858ccc02172ea to your computer and use it in GitHub Desktop.
Useful code
"""
Use this script to debug an application served by Uvicorn.
"""
# Nota bene: to debug an application that configures an event loop before calling uvicorn.run,
# it is possible to replace `uvicorn.run` with the following code fragment.
# This is necessary for example, when data access layer and business logic
# are configured before starting the application.
# See https://github.com/encode/uvicorn/pull/446#issuecomment-540484961
from uvicorn import Server, Config
from uvicorn.supervisors import Multiprocess, StatReload
from server import app
class DebugConfig(Config):
def setup_event_loop(self):
...
def run(application, **kwargs):
config = DebugConfig(application, **kwargs)
server = Server(config=config)
if config.reload and not isinstance(application, str):
config.logger_instance.warn(
"auto-reload only works when app is passed as an import string."
)
if isinstance(application, str) and (config.debug or config.reload):
sock = config.bind_socket()
supervisor = StatReload(config)
supervisor.run(server.run, sockets=[sock])
elif config.workers > 1:
sock = config.bind_socket()
supervisor = Multiprocess(config)
supervisor.run(server.run, sockets=[sock])
else:
server.run()
run(app, host='127.0.0.1', port=44777, log_level='info')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment