Problem enabling Uvicorn auto-restart when launching programmatically with uvicorn.run
Asked Answered
C

2

14

I'm attempting to get Uvicorn to automatically restart on detected file changes when launching programmatically, as it would when started from the command line with the --debug switch. The following statement is at the bottom of my api source code file and while Uvicorn starts and runs fine, it doesn't launch in reload mode. I've tried setting the debug parameter to various diffrent values: uvicorn.run(debug= 'true', 'True', 'yes' and True (python boolean), but nothing seems to work.

uvicorn.run(app,
            host=run_config['api_host'],
            port=run_config['api_port'],
            log_level=run_config['log_level'],
            debug='true')

EDIT: In reference to my comment on @howderek's answer: I've tried a modified version of the suggested code. While the server successfully starts, the code below doesn't turn on the reloader:

import uvicorn
from uvicorn.reloaders.statreload import StatReload
reloader = StatReload('debug')
reloader.run(uvicorn.run(app, host='localhost', port=9187, debug='true'))
Chita answered 12/10, 2018 at 18:5 Comment(0)
P
9

That is because the --debug flag does more than just set debug=True in the run function.

In the Uvicorn source it appears they are creating a StatReload which was imported from uvicorn.reloaders.statreload

I couldn't find any documentation regarding this feature, but it appears all you will need to do is take:

uvicorn.run(app,
    host=run_config['api_host'],
    port=run_config['api_port'],
    log_level=run_config['log_level'],
    debug='true')

and make it:

from uvicorn.reloaders.statreload import StatReload
from uvicorn.main import run, get_logger
reloader = StatReload(get_logger(run_config['log_level']))
reloader.run(run, {
    'app': app,
    'host': run_config['api_host'],
    'port': run_config['api_port'],
    'log_level': run_config['log_level'],
    'debug': 'true'
})
Playback answered 12/10, 2018 at 18:16 Comment(2)
I'm pretty sure this is in the right direction but as written it doesn't quite work because uivicorn has no method get_logger (but StatReload itself does take a logger parameter). Also the asgi app name is not called.Chita
Works perfectly just as written above in the edit. Many thanks!!Chita
C
21

The documentation states that you can just use reload=True.

Example:

uvicorn.run("example:app", port=5000, reload=True, access_log=False)
Crayfish answered 16/2, 2021 at 14:12 Comment(0)
P
9

That is because the --debug flag does more than just set debug=True in the run function.

In the Uvicorn source it appears they are creating a StatReload which was imported from uvicorn.reloaders.statreload

I couldn't find any documentation regarding this feature, but it appears all you will need to do is take:

uvicorn.run(app,
    host=run_config['api_host'],
    port=run_config['api_port'],
    log_level=run_config['log_level'],
    debug='true')

and make it:

from uvicorn.reloaders.statreload import StatReload
from uvicorn.main import run, get_logger
reloader = StatReload(get_logger(run_config['log_level']))
reloader.run(run, {
    'app': app,
    'host': run_config['api_host'],
    'port': run_config['api_port'],
    'log_level': run_config['log_level'],
    'debug': 'true'
})
Playback answered 12/10, 2018 at 18:16 Comment(2)
I'm pretty sure this is in the right direction but as written it doesn't quite work because uivicorn has no method get_logger (but StatReload itself does take a logger parameter). Also the asgi app name is not called.Chita
Works perfectly just as written above in the edit. Many thanks!!Chita

© 2022 - 2024 — McMap. All rights reserved.