Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Add example how to run the server programmatically with reload" #1761

Merged
merged 1 commit into from Nov 11, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -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'

Expand All @@ -74,7 +74,7 @@ async def app(scope, receive, send):
Run the server:

```shell
$ uvicorn main:app
$ uvicorn example:app
```

---
Expand Down
34 changes: 4 additions & 30 deletions docs/deployment.md
Expand Up @@ -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:
Expand All @@ -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.
Expand All @@ -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.
Expand Down
19 changes: 11 additions & 8 deletions docs/index.md
Expand Up @@ -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'

Expand All @@ -81,7 +81,7 @@ async def app(scope, receive, send):
Run the server:

```shell
$ uvicorn main:app
$ uvicorn example:app
```

---
Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand All @@ -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

Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion mkdocs.yml
Expand Up @@ -29,7 +29,6 @@ nav:
- Contributing: "contributing.md"

markdown_extensions:
- pymdownx.superfences
- admonition
- codehilite:
css_class: highlight
Expand Down