From 6496e6c1e3647c852bf993f7d62f2c65f9a8c2f5 Mon Sep 17 00:00:00 2001 From: Marcelo Trylesinski Date: Fri, 11 Nov 2022 06:40:48 +0100 Subject: [PATCH] Revert "Add example how to run the server programmatically with reload" (#1761) --- README.md | 6 +++--- docs/deployment.md | 34 ++++------------------------------ docs/index.md | 19 +++++++++++-------- mkdocs.yml | 1 - 4 files changed, 18 insertions(+), 42 deletions(-) diff --git a/README.md b/README.md index e17fa1626..9617acab5 100644 --- a/README.md +++ b/README.md @@ -52,9 +52,9 @@ Moreover, "optional extras" means that: - `python-dotenv` will be installed should you want to use the `--env-file` option. - `PyYAML` will be installed to allow you to provide a `.yaml` file to `--log-config`, if desired. -Create an application: +Create an application, in `example.py`: -```py title="main.py" +```python async def app(scope, receive, send): assert scope['type'] == 'http' @@ -74,7 +74,7 @@ async def app(scope, receive, send): Run the server: ```shell -$ uvicorn main:app +$ uvicorn example:app ``` --- diff --git a/docs/deployment.md b/docs/deployment.md index f41057504..65c6f0e9a 100644 --- a/docs/deployment.md +++ b/docs/deployment.md @@ -135,7 +135,9 @@ See the [settings documentation](settings.md) for more details on the supported To run directly from within a Python program, you should use `uvicorn.run(app, **config)`. For example: -```py title="main.py" +**example.py**: + +```python import uvicorn class App: @@ -144,7 +146,7 @@ class App: app = App() if __name__ == "__main__": - uvicorn.run("main:app", host="127.0.0.1", port=5000, log_level="info") + uvicorn.run("example:app", host="127.0.0.1", port=5000, log_level="info") ``` The set of configuration options is the same as for the command line tool. @@ -164,34 +166,6 @@ Also note that in this case, you should put `uvicorn.run` into `if __name__ == ' !!! note The `reload` and `workers` parameters are **mutually exclusive**. - -To run the server programmatically with the `reload` option, you should additionally use the `ChangeReload` supervisor. For example: - -```py title="main.py" -import os -import uvicorn -from uvicorn.supervisors import ChangeReload - -class App: - ... - -app = App() - -if __name__ == "__main__": - reload_dir = os.path.dirname(__file__) - config = uvicorn.Config( - "main:app", - host="127.0.0.1", - port=5000, - log_level="info", - reload=True, - reload_dirs=[reload_dir] - ) - server = uvicorn.Server(config) - sock = config.bind_socket() - ChangeReload(config, target=server.run, sockets=[sock]).run() -``` - ## Using a process manager Running Uvicorn using a process manager ensures that you can run multiple processes in a resilient manner, and allows you to perform server upgrades without dropping requests. diff --git a/docs/index.md b/docs/index.md index bbff81bce..945b80772 100644 --- a/docs/index.md +++ b/docs/index.md @@ -59,9 +59,9 @@ Moreover, "optional extras" means that: - `python-dotenv` will be installed should you want to use the `--env-file` option. - `PyYAML` will be installed to allow you to provide a `.yaml` file to `--log-config`, if desired. -Create an application: +Create an application, in `example.py`: -```py title="main.py" +```python async def app(scope, receive, send): assert scope['type'] == 'http' @@ -81,7 +81,7 @@ async def app(scope, receive, send): Run the server: ```shell -$ uvicorn main:app +$ uvicorn example:app ``` --- @@ -205,7 +205,8 @@ There are several ways to run uvicorn directly from your application. If you're looking for a programmatic equivalent of the `uvicorn` command line interface, use `uvicorn.run()`: -```py title="main.py" +```python +# main.py import uvicorn async def app(scope, receive, send): @@ -219,7 +220,7 @@ if __name__ == "__main__": For more control over configuration and server lifecycle, use `uvicorn.Config` and `uvicorn.Server`: -```py title="main.py" +```python import uvicorn async def app(scope, receive, send): @@ -233,7 +234,7 @@ if __name__ == "__main__": If you'd like to run Uvicorn from an already running async environment, use `uvicorn.Server.serve()` instead: -```py title="main.py" +```python import asyncio import uvicorn @@ -274,14 +275,16 @@ For more information, see the [deployment documentation](deployment.md). The `--factory` flag allows loading the application from a factory function, rather than an application instance directly. The factory will be called with no arguments and should return an ASGI application. -```py title="main.py" +**example.py**: + +```python def create_app(): app = ... return app ``` ```shell -$ uvicorn --factory main:create_app +$ uvicorn --factory example:create_app ``` ## The ASGI interface diff --git a/mkdocs.yml b/mkdocs.yml index ddb311987..ed833a10c 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -29,7 +29,6 @@ nav: - Contributing: "contributing.md" markdown_extensions: - - pymdownx.superfences - admonition - codehilite: css_class: highlight