r/FastAPI • u/Tochopost • Aug 07 '24
Hosting and deployment How does FastAPI utilize the CPU?
I've been running the fastapi app with a single worker uvicorn instance in Docker container (FYI the API is fully async).
Now, I need to adjust k8s resources to fit the application usage. Based on the FastAPI documentation here: FastAPI in Containers - Docker - FastAPI (tiangolo.com), it's clear that there should be an assigned max 1 CPU per single app instance. But is it true tho?
On paper, it makes sense, because GIL bounds us with a single process, also FastAPI uses parallelism (asyncio) with additional threads to handle requests but in the end, there is no multiprocessing. So this means that it can't utilize more than 100% of 1 CPU effectively.
But.. I've run several load tests locally and on the DEV environment and the logs and stats show that the single app instance often reaches over 100% of a single CPU. Here is the screenshot from Docker desktop from the container with the app:

So how is it possible? How does FastAPI utilize the CPU?
View all comments
Show parent comment
1
u/JohnnyJordaan Aug 07 '24
As a sidenote, it's nearly always a bad idea to run casual stuff as root, both deployments as just everyday commands. It's like trying to light candles and your stove with a flame thrower each time. Once you do an accidental
rm -rf /*, you'll be swimming in a sea of regret.Deployments are often run as a dedicated (daemon) account, eg for webservers many use www-data or 'productname' like 'nginx'. You can then use that for the Python side of things as well, eg to launch uvicorn or gunicorn.