I am trying to create a click command that starts a uvicorn server but can't get the server to start. I get a feeling it may be related to asyncio, which I am not very familiar with it. Any help is appreciated
Works as Expected
>>> import uvicorn
>>> uvicorn.run("downloader.mock_server:app")
INFO: Started server process [84462]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
Click Command
@cli.command()
@click.option("--host", default="http://0.0.0.0", help="host")
@click.option("--port", default=8000, help="port")
@click.option("--reload", is_flag=True, help="Should Reload")
def mock_server(host, port, reload):
"""Starts Mock server"""
uvicorn.run("downloader.mock_server:app", host=host, port=port, reload=False)
print("Help!") #never reaches this line + server does not start
Running Command:
>>> ~
.venv ❯ downloader mock-server
Starting Server
.venv ❯
With click, program exits at run() step, event if I try putting a breakpoint after nothing happens.
I also tried using the decorator provided here but it did not help:
.venv ❯ downloader mock-server
sys:1: RuntimeWarning: coroutine 'mock_server' was never awaited
RuntimeWarning: Enable tracemalloc to get the object allocation traceback