Compare commits

...

2 Commits

Author SHA1 Message Date
Marcelo Trylesinski
6ca58f9486
Merge branch 'master' into feat/error-msg-on-multiprocessing 2023-12-03 13:08:44 +00:00
Marcelo Trylesinski
711f4a3e60 Add error message when running uvicorn programatically with multiprocesses 2023-06-14 10:03:57 +02:00
2 changed files with 10 additions and 7 deletions

View File

@ -4,8 +4,6 @@ Use the following options to configure Uvicorn, when running from the command li
If you're running programmatically, using `uvicorn.run(...)`, then use
equivalent keyword arguments, eg. `uvicorn.run("example:app", port=5000, reload=True, access_log=False)`.
Please note that in this case, if you use `reload=True` or `workers=NUM`,
you should put `uvicorn.run` into `if __name__ == '__main__'` clause in the main module.
You can also configure Uvicorn using environment variables with the prefix `UVICORN_`.
For example, in case you want to run the app on port `5000`, just set the environment variable `UVICORN_PORT` to `5000`.

View File

@ -1,5 +1,6 @@
import asyncio
import logging
import multiprocessing
import os
import platform
import ssl
@ -569,13 +570,17 @@ def run(
)
server = Server(config=config)
if (config.reload or config.workers > 1) and not isinstance(app, str):
is_main_process = multiprocessing.current_process().name == "MainProcess"
# fmt: off
if (config.reload or config.workers > 1) and not isinstance(app, str) or not is_main_process: # noqa
logger = logging.getLogger("uvicorn.error")
logger.warning(
"You must pass the application as an import string to enable 'reload' or "
"'workers'."
)
if not isinstance(app, str):
msg = "You must pass the application as an import string to enable 'reload' or 'workers'." # noqa: E501
elif not is_main_process:
msg = "You must run `uvicorn.run()` inside a `if __name__ == '__main__'` block." # noqa: E501
logger.error(msg)
sys.exit(1)
# fmt: on
if config.should_reload:
sock = config.bind_socket()