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 aserver program, for example, using thefastapi command, that runs Uvicorn, running asingle process.
When deploying applications you will probably want to have somereplication of processes to take advantage ofmultiple cores and to be able to handle more requests.
As you saw in the previous chapter aboutDeployment Concepts, there are multiple strategies you can use.
Here I'll show you how to useUvicorn withworker processes using thefastapi command or theuvicorn 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.
In particular, when running onKubernetes you will probablynot want to use workers and instead runa single Uvicorn process per container, but I'll tell you about it later in that chapter.
Multiple Workers¶
You can start multiple workers with the--workers command line option:
If you use thefastapi command:
$<fontcolor="#4E9A06">fastapi</font>run--workers4<ustyle="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.If you prefer to use theuvicorn command directly:
$uvicornmain:app--host0.0.0.0--port8080--workers4<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.The only new option here is--workers telling Uvicorn to start 4 worker processes.
You can also see that it shows thePID of each process,27365 for the parent process (this is theprocess manager) and one for each worker process:27368,27369,27370, and27367.
Deployment Concepts¶
Here you saw how to use multipleworkers toparallelize the execution of the application, take advantage ofmultiple cores in the CPU, and be able to servemore requests.
From the list of deployment concepts from above, using workers would mainly help with thereplication part, and a little bit with therestarts, 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¶
In the next chapter aboutFastAPI in Containers - Docker I'll explain some strategies you could use to handle the otherdeployment concepts.
I'll show you how tobuild 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 likeKubernetes.
Recap¶
You can use multiple worker processes with the--workers CLI option with thefastapi oruvicorn commands to take advantage ofmulti-core CPUs, to runmultiple processes in parallel.
You could use these tools and ideas if you are setting upyour own deployment system while taking care of the other deployment concepts yourself.
Check out the next chapter to learn aboutFastAPI with containers (e.g. Docker and Kubernetes). You will see that those tools have simple ways to solve the otherdeployment concepts as well. ✨







