You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/en/docs/deployment/concepts.md
+3-5Lines changed: 3 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -94,7 +94,7 @@ In most cases, when you create a web API, you want it to be **always running**,
94
94
95
95
### In a Remote Server
96
96
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.
98
98
99
99
And it will work and will be useful **during development**.
100
100
@@ -178,7 +178,7 @@ For example, this could be handled by:
178
178
179
179
## Replication - Processes and Memory
180
180
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.
182
182
183
183
But in many cases, you will want to run several worker processes at the same time.
184
184
@@ -232,9 +232,7 @@ The main constraint to consider is that there has to be a **single** component h
232
232
233
233
Here are some possible combinations and strategies:
234
234
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`
238
236
* One Uvicorn **process manager** would listen on the **IP** and **port**, and it would start **multiple Uvicorn worker processes**.
239
237
***Kubernetes** and other distributed **container systems**
240
238
* 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.
Copy file name to clipboardExpand all lines: docs/en/docs/deployment/manually.md
+9-80Lines changed: 9 additions & 80 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -67,6 +67,8 @@ There are several alternatives, including:
67
67
* <ahref="https://www.uvicorn.org/"class="external-link"target="_blank">Uvicorn</a>: a high performance ASGI server.
68
68
* <ahref="https://hypercorn.readthedocs.io/"class="external-link"target="_blank">Hypercorn</a>: an ASGI server compatible with HTTP/2 and Trio among other features.
69
69
* <ahref="https://github.com/django/daphne"class="external-link"target="_blank">Daphne</a>: the ASGI server built for Django Channels.
70
+
* <ahref="https://github.com/emmett-framework/granian"class="external-link"target="_blank">Granian</a>: A Rust HTTP server for Python applications.
71
+
* <ahref="https://unit.nginx.org/howto/fastapi/"class="external-link"target="_blank">NGINX Unit</a>: NGINX Unit is a lightweight and versatile web application runtime.
70
72
71
73
## Server Machine and Server Program
72
74
@@ -84,11 +86,9 @@ When you install FastAPI, it comes with a production server, Uvicorn, and you ca
84
86
85
87
But you can also install an ASGI server manually.
86
88
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.
88
90
89
-
//// tab | Uvicorn
90
-
91
-
* <ahref="https://www.uvicorn.org/"class="external-link"target="_blank">Uvicorn</a>, a lightning-fast ASGI server, built on uvloop and httptools.
A similar process would apply to any other ASGI server program.
104
+
103
105
/// tip
104
106
105
107
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
110
112
111
113
///
112
114
113
-
////
114
-
115
-
//// tab | Hypercorn
116
-
117
-
* <ahref="https://github.com/pgjones/hypercorn"class="external-link"target="_blank">Hypercorn</a>, an ASGI server also compatible with HTTP/2.
118
-
119
-
<divclass="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
-
133
115
## Run the Server Program
134
116
135
117
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:
Running on 0.0.0.0:8080 over http (CTRL + C to quit)
159
-
```
160
-
161
-
</div>
162
-
163
-
////
164
-
165
129
/// note
166
130
167
131
The command `uvicorn main:app` refers to:
@@ -177,53 +141,18 @@ from main import app
177
141
178
142
///
179
143
144
+
Each alternative ASGI server program would have a similar command, you can read more in their respective documentation.
145
+
180
146
/// warning
181
147
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.
183
149
184
150
The `--reload` option consumes much more resources, is more unstable, etc.
185
151
186
152
It helps a lot during **development**, but you **shouldn't** use it in **production**.
187
153
188
154
///
189
155
190
-
## Hypercorn with Trio
191
-
192
-
Starlette and **FastAPI** are based on <ahref="https://anyio.readthedocs.io/en/stable/"class="external-link"target="_blank">AnyIO</a>, which makes them compatible with both Python's standard library <ahref="https://docs.python.org/3/library/asyncio-task.html"class="external-link"target="_blank">asyncio</a> and <ahref="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 <ahref="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
-
<divclass="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
-
<divclass="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
-
227
156
## Deployment Concepts
228
157
229
158
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