mirror of https://github.com/tiangolo/fastapi.git
140 lines
8.3 KiB
Markdown
140 lines
8.3 KiB
Markdown
# Server Workers - Uvicorn with Workers { #server-workers-uvicorn-with-workers }
|
|
|
|
Let's check back those deployment concepts from before:
|
|
|
|
* Security - HTTPS
|
|
* Running on startup
|
|
* Restarts
|
|
* **Replication (the number of processes running)**
|
|
* Memory
|
|
* Previous steps before starting
|
|
|
|
Up to this point, with all the tutorials in the docs, you have probably been running a **server program**, for example, using the `fastapi` command, that runs Uvicorn, running a **single process**.
|
|
|
|
When deploying applications you will probably want to have some **replication of processes** to take advantage of **multiple cores** and to be able to handle more requests.
|
|
|
|
As you saw in the previous chapter about [Deployment Concepts](concepts.md){.internal-link target=_blank}, there are multiple strategies you can use.
|
|
|
|
Here I'll show you how to use **Uvicorn** with **worker processes** using the `fastapi` command or the `uvicorn` command directly.
|
|
|
|
/// info
|
|
|
|
If you are using containers, for example with Docker or Kubernetes, I'll tell you more about that in the next chapter: [FastAPI in Containers - Docker](docker.md){.internal-link target=_blank}.
|
|
|
|
In particular, when running on **Kubernetes** you will probably **not** want to use workers and instead run **a single Uvicorn process per container**, but I'll tell you about it later in that chapter.
|
|
|
|
///
|
|
|
|
## Multiple Workers { #multiple-workers }
|
|
|
|
You can start multiple workers with the `--workers` command line option:
|
|
|
|
//// tab | `fastapi`
|
|
|
|
If you use the `fastapi` command:
|
|
|
|
<div class="termy">
|
|
|
|
```console
|
|
$ <font color="#4E9A06">fastapi</font> run --workers 4 <u style="text-decoration-style:solid">main.py</u>
|
|
|
|
<span style="background-color:#009485"><font color="#D3D7CF"> FastAPI </font></span> Starting production server 🚀
|
|
|
|
Searching for package file structure from directories with
|
|
<font color="#3465A4">__init__.py</font> files
|
|
Importing from <font color="#75507B">/home/user/code/</font><font color="#AD7FA8">awesomeapp</font>
|
|
|
|
<span style="background-color:#007166"><font color="#D3D7CF"> module </font></span> 🐍 main.py
|
|
|
|
<span style="background-color:#007166"><font color="#D3D7CF"> code </font></span> Importing the FastAPI app object from the module with the
|
|
following code:
|
|
|
|
<u style="text-decoration-style:solid">from </u><u style="text-decoration-style:solid"><b>main</b></u><u style="text-decoration-style:solid"> import </u><u style="text-decoration-style:solid"><b>app</b></u>
|
|
|
|
<span style="background-color:#007166"><font color="#D3D7CF"> app </font></span> Using import string: <font color="#3465A4">main:app</font>
|
|
|
|
<span style="background-color:#007166"><font color="#D3D7CF"> server </font></span> Server started at <font color="#729FCF"><u style="text-decoration-style:solid">http://0.0.0.0:8000</u></font>
|
|
<span style="background-color:#007166"><font color="#D3D7CF"> server </font></span> Documentation at <font color="#729FCF"><u style="text-decoration-style:solid">http://0.0.0.0:8000/docs</u></font>
|
|
|
|
Logs:
|
|
|
|
<span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span> Uvicorn running on <font color="#729FCF"><u style="text-decoration-style:solid">http://0.0.0.0:8000</u></font> <b>(</b>Press CTRL+C to
|
|
quit<b>)</b>
|
|
<span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span> Started parent process <b>[</b><font color="#34E2E2"><b>27365</b></font><b>]</b>
|
|
<span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span> Started server process <b>[</b><font color="#34E2E2"><b>27368</b></font><b>]</b>
|
|
<span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span> Started server process <b>[</b><font color="#34E2E2"><b>27369</b></font><b>]</b>
|
|
<span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span> Started server process <b>[</b><font color="#34E2E2"><b>27370</b></font><b>]</b>
|
|
<span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span> Started server process <b>[</b><font color="#34E2E2"><b>27367</b></font><b>]</b>
|
|
<span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span> Waiting for application startup.
|
|
<span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span> Waiting for application startup.
|
|
<span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span> Waiting for application startup.
|
|
<span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span> Waiting for application startup.
|
|
<span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span> Application startup complete.
|
|
<span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span> Application startup complete.
|
|
<span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span> Application startup complete.
|
|
<span style="background-color:#007166"><font color="#D3D7CF"> INFO </font></span> Application startup complete.
|
|
```
|
|
|
|
</div>
|
|
|
|
////
|
|
|
|
//// tab | `uvicorn`
|
|
|
|
If you prefer to use the `uvicorn` command directly:
|
|
|
|
<div class="termy">
|
|
|
|
```console
|
|
$ uvicorn main:app --host 0.0.0.0 --port 8080 --workers 4
|
|
<font color="#A6E22E">INFO</font>: Uvicorn running on <b>http://0.0.0.0:8080</b> (Press CTRL+C to quit)
|
|
<font color="#A6E22E">INFO</font>: Started parent process [<font color="#A1EFE4"><b>27365</b></font>]
|
|
<font color="#A6E22E">INFO</font>: Started server process [<font color="#A1EFE4">27368</font>]
|
|
<font color="#A6E22E">INFO</font>: Waiting for application startup.
|
|
<font color="#A6E22E">INFO</font>: Application startup complete.
|
|
<font color="#A6E22E">INFO</font>: Started server process [<font color="#A1EFE4">27369</font>]
|
|
<font color="#A6E22E">INFO</font>: Waiting for application startup.
|
|
<font color="#A6E22E">INFO</font>: Application startup complete.
|
|
<font color="#A6E22E">INFO</font>: Started server process [<font color="#A1EFE4">27370</font>]
|
|
<font color="#A6E22E">INFO</font>: Waiting for application startup.
|
|
<font color="#A6E22E">INFO</font>: Application startup complete.
|
|
<font color="#A6E22E">INFO</font>: Started server process [<font color="#A1EFE4">27367</font>]
|
|
<font color="#A6E22E">INFO</font>: Waiting for application startup.
|
|
<font color="#A6E22E">INFO</font>: Application startup complete.
|
|
```
|
|
|
|
</div>
|
|
|
|
////
|
|
|
|
The only new option here is `--workers` telling Uvicorn to start 4 worker processes.
|
|
|
|
You can also see that it shows the **PID** of each process, `27365` for the parent process (this is the **process manager**) and one for each worker process: `27368`, `27369`, `27370`, and `27367`.
|
|
|
|
## Deployment Concepts { #deployment-concepts }
|
|
|
|
Here you saw how to use multiple **workers** to **parallelize** the execution of the application, take advantage of **multiple cores** in the CPU, and be able to serve **more requests**.
|
|
|
|
From the list of deployment concepts from above, using workers would mainly help with the **replication** part, and a little bit with the **restarts**, but you still need to take care of the others:
|
|
|
|
* **Security - HTTPS**
|
|
* **Running on startup**
|
|
* ***Restarts***
|
|
* Replication (the number of processes running)
|
|
* **Memory**
|
|
* **Previous steps before starting**
|
|
|
|
## Containers and Docker { #containers-and-docker }
|
|
|
|
In the next chapter about [FastAPI in Containers - Docker](docker.md){.internal-link target=_blank} I'll explain some strategies you could use to handle the other **deployment concepts**.
|
|
|
|
I'll show you how to **build your own image from scratch** to run a single Uvicorn process. It is a simple process and is probably what you would want to do when using a distributed container management system like **Kubernetes**.
|
|
|
|
## Recap { #recap }
|
|
|
|
You can use multiple worker processes with the `--workers` CLI option with the `fastapi` or `uvicorn` commands to take advantage of **multi-core CPUs**, to run **multiple processes in parallel**.
|
|
|
|
You could use these tools and ideas if you are setting up **your own deployment system** while taking care of the other deployment concepts yourself.
|
|
|
|
Check out the next chapter to learn about **FastAPI** with containers (e.g. Docker and Kubernetes). You will see that those tools have simple ways to solve the other **deployment concepts** as well. ✨
|