Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit bd1b775

Browse files
authored
πŸ“ Update docs about serving FastAPI: ASGI servers, Docker containers, etc. (#12069)
1 parent b5cbff9 commit bd1b775

9 files changed

Lines changed: 129 additions & 373 deletions

File tree

β€Ždocs/en/docs/advanced/sub-applications.mdβ€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@ In this case, it will be mounted at the path `/subapi`:
3636

3737
### Check the automatic API docs
3838

39-
Now, run `uvicorn` with the main app, if your file is `main.py`, it would be:
39+
Now, run the `fastapi` command with your file:
4040

4141
<div class="termy">
4242

4343
```console
44-
$ uvicorn main:app --reload
44+
$ fastapi dev main.py
4545

4646
<span style="color: green;">INFO</span>: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
4747
```

β€Ždocs/en/docs/alternatives.mdβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ It is the recommended server for Starlette and **FastAPI**.
474474

475475
The main web server to run **FastAPI** applications.
476476

477-
You can combine it with Gunicorn, to have an asynchronous multi-process server.
477+
You can also use the `--workers` command line option to have an asynchronous multi-process server.
478478

479479
Check more details in the [Deployment](deployment/index.md){.internal-link target=_blank} section.
480480

β€Ždocs/en/docs/contributing.mdβ€Ž

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ If you run the examples with, e.g.:
170170
<div class="termy">
171171

172172
```console
173-
$ uvicorn tutorial001:app --reload
173+
$ fastapi dev tutorial001.py
174174

175175
<span style="color: green;">INFO</span>: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
176176
```

β€Ždocs/en/docs/deployment/concepts.mdβ€Ž

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ In most cases, when you create a web API, you want it to be **always running**,
9494

9595
### In a Remote Server
9696

97-
When you set up a remote server (a cloud server, a virtual machine, etc.) the simplest thing you can do is to use `fastapi run`, Uvicorn (or similar) manually, the same way you do when developing locally.
97+
When you set up a remote server (a cloud server, a virtual machine, etc.) the simplest thing you can do is use `fastapi run` (which uses Uvicorn) or something similar, manually, the same way you do when developing locally.
9898

9999
And it will work and will be useful **during development**.
100100

@@ -178,7 +178,7 @@ For example, this could be handled by:
178178

179179
## Replication - Processes and Memory
180180

181-
With a FastAPI application, using a server program like Uvicorn, running it once in **one process** can serve multiple clients concurrently.
181+
With a FastAPI application, using a server program like the `fastapi` command that runs Uvicorn, running it once in **one process** can serve multiple clients concurrently.
182182

183183
But in many cases, you will want to run several worker processes at the same time.
184184

@@ -232,9 +232,7 @@ The main constraint to consider is that there has to be a **single** component h
232232

233233
Here are some possible combinations and strategies:
234234

235-
* **Gunicorn** managing **Uvicorn workers**
236-
* Gunicorn would be the **process manager** listening on the **IP** and **port**, the replication would be by having **multiple Uvicorn worker processes**.
237-
* **Uvicorn** managing **Uvicorn workers**
235+
* **Uvicorn** with `--workers`
238236
* One Uvicorn **process manager** would listen on the **IP** and **port**, and it would start **multiple Uvicorn worker processes**.
239237
* **Kubernetes** and other distributed **container systems**
240238
* Something in the **Kubernetes** layer would listen on the **IP** and **port**. The replication would be by having **multiple containers**, each with **one Uvicorn process** running.

β€Ždocs/en/docs/deployment/docker.mdβ€Ž

Lines changed: 44 additions & 182 deletions
Large diffs are not rendered by default.

β€Ždocs/en/docs/deployment/manually.mdβ€Ž

Lines changed: 9 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ There are several alternatives, including:
6767
* <a href="https://www.uvicorn.org/" class="external-link" target="_blank">Uvicorn</a>: a high performance ASGI server.
6868
* <a href="https://hypercorn.readthedocs.io/" class="external-link" target="_blank">Hypercorn</a>: an ASGI server compatible with HTTP/2 and Trio among other features.
6969
* <a href="https://github.com/django/daphne" class="external-link" target="_blank">Daphne</a>: the ASGI server built for Django Channels.
70+
* <a href="https://github.com/emmett-framework/granian" class="external-link" target="_blank">Granian</a>: A Rust HTTP server for Python applications.
71+
* <a href="https://unit.nginx.org/howto/fastapi/" class="external-link" target="_blank">NGINX Unit</a>: NGINX Unit is a lightweight and versatile web application runtime.
7072

7173
## Server Machine and Server Program
7274

@@ -84,11 +86,9 @@ When you install FastAPI, it comes with a production server, Uvicorn, and you ca
8486

8587
But you can also install an ASGI server manually.
8688

87-
Make sure you create a [virtual environment](../virtual-environments.md){.internal-link target=_blank}, activate it, and then you can install the server:
89+
Make sure you create a [virtual environment](../virtual-environments.md){.internal-link target=_blank}, activate it, and then you can install the server application.
8890

89-
//// tab | Uvicorn
90-
91-
* <a href="https://www.uvicorn.org/" class="external-link" target="_blank">Uvicorn</a>, a lightning-fast ASGI server, built on uvloop and httptools.
91+
For example, to install Uvicorn:
9292

9393
<div class="termy">
9494

@@ -100,6 +100,8 @@ $ pip install "uvicorn[standard]"
100100

101101
</div>
102102

103+
A similar process would apply to any other ASGI server program.
104+
103105
/// tip
104106

105107
By adding the `standard`, Uvicorn will install and use some recommended extra dependencies.
@@ -110,32 +112,10 @@ When you install FastAPI with something like `pip install "fastapi[standard]"` y
110112

111113
///
112114

113-
////
114-
115-
//// tab | Hypercorn
116-
117-
* <a href="https://github.com/pgjones/hypercorn" class="external-link" target="_blank">Hypercorn</a>, an ASGI server also compatible with HTTP/2.
118-
119-
<div class="termy">
120-
121-
```console
122-
$ pip install hypercorn
123-
124-
---> 100%
125-
```
126-
127-
</div>
128-
129-
...or any other ASGI server.
130-
131-
////
132-
133115
## Run the Server Program
134116

135117
If you installed an ASGI server manually, you would normally need to pass an import string in a special format for it to import your FastAPI application:
136118

137-
//// tab | Uvicorn
138-
139119
<div class="termy">
140120

141121
```console
@@ -146,22 +126,6 @@ $ uvicorn main:app --host 0.0.0.0 --port 80
146126

147127
</div>
148128

149-
////
150-
151-
//// tab | Hypercorn
152-
153-
<div class="termy">
154-
155-
```console
156-
$ hypercorn main:app --bind 0.0.0.0:80
157-
158-
Running on 0.0.0.0:8080 over http (CTRL + C to quit)
159-
```
160-
161-
</div>
162-
163-
////
164-
165129
/// note
166130

167131
The command `uvicorn main:app` refers to:
@@ -177,53 +141,18 @@ from main import app
177141

178142
///
179143

144+
Each alternative ASGI server program would have a similar command, you can read more in their respective documentation.
145+
180146
/// warning
181147

182-
Uvicorn and others support a `--reload` option that is useful during development.
148+
Uvicorn and other servers support a `--reload` option that is useful during development.
183149

184150
The `--reload` option consumes much more resources, is more unstable, etc.
185151

186152
It helps a lot during **development**, but you **shouldn't** use it in **production**.
187153

188154
///
189155

190-
## Hypercorn with Trio
191-
192-
Starlette and **FastAPI** are based on <a href="https://anyio.readthedocs.io/en/stable/" class="external-link" target="_blank">AnyIO</a>, which makes them compatible with both Python's standard library <a href="https://docs.python.org/3/library/asyncio-task.html" class="external-link" target="_blank">asyncio</a> and <a href="https://trio.readthedocs.io/en/stable/" class="external-link" target="_blank">Trio</a>.
193-
194-
Nevertheless, Uvicorn is currently only compatible with asyncio, and it normally uses <a href="https://github.com/MagicStack/uvloop" class="external-link" target="_blank">`uvloop`</a>, the high-performance drop-in replacement for `asyncio`.
195-
196-
But if you want to directly use **Trio**, then you can use **Hypercorn** as it supports it. ✨
197-
198-
### Install Hypercorn with Trio
199-
200-
First you need to install Hypercorn with Trio support:
201-
202-
<div class="termy">
203-
204-
```console
205-
$ pip install "hypercorn[trio]"
206-
---> 100%
207-
```
208-
209-
</div>
210-
211-
### Run with Trio
212-
213-
Then you can pass the command line option `--worker-class` with the value `trio`:
214-
215-
<div class="termy">
216-
217-
```console
218-
$ hypercorn main:app --worker-class trio
219-
```
220-
221-
</div>
222-
223-
And that will start Hypercorn with your app using Trio as the backend.
224-
225-
Now you can use Trio internally in your app. Or even better, you can use AnyIO, to keep your code compatible with both Trio and asyncio. πŸŽ‰
226-
227156
## Deployment Concepts
228157

229158
These examples run the server program (e.g Uvicorn), starting **a single process**, listening on all the IPs (`0.0.0.0`) on a predefined port (e.g. `80`).

0 commit comments

Comments
Β (0)