question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[QUESTION] FastAPI keeps booting new workers when deploying

See original GitHub issue

Hi there,

I’m trying to run two different instances of FastAPI in the same Kubernetes pod (different Docker containers), but one of the two seems to keep booting up new workers. Therefor, the container uses a lot of CPU for nothing. It’s also reporting that it’s waiting for application startup constantly, but it is reachable (but slow).

Here is the log that I keep getting:

Checking for script in /app/prestart.sh
Running script /app/prestart.sh
Running inside /app/prestart.sh, you could add migrations to this file, e.g.:
 #! /usr/bin/env bash
 # Let the DB start
sleep 10;
# Run migrations
alembic upgrade head
 [2019-05-24 11:51:24 +0000] [1] [INFO] Starting gunicorn 19.9.0
[2019-05-24 11:51:24 +0000] [1] [INFO] Listening at: http://0.0.0.0:8081 (1)
[2019-05-24 11:51:24 +0000] [1] [INFO] Using worker: uvicorn.workers.UvicornWorker
[2019-05-24 11:51:24 +0000] [8] [INFO] Booting worker with pid: 8
[2019-05-24 11:51:24 +0000] [9] [INFO] Booting worker with pid: 9
WARNING:root:email-validator not installed, email fields will be treated as str.
To install, run: pip install email-validator
WARNING:root:email-validator not installed, email fields will be treated as str.
To install, run: pip install email-validator
[2019-05-24 11:51:33 +0000] [9] [INFO] Started server process [9]
[2019-05-24 11:51:33 +0000] [9] [INFO] Waiting for application startup.
[2019-05-24 11:51:33 +0000] [8] [INFO] Started server process [8]
[2019-05-24 11:51:33 +0000] [8] [INFO] Waiting for application startup.
[2019-05-24 11:51:37 +0000] [18] [INFO] Booting worker with pid: 18
WARNING:root:email-validator not installed, email fields will be treated as str.
To install, run: pip install email-validator
[2019-05-24 11:51:44 +0000] [18] [INFO] Started server process [18]
[2019-05-24 11:51:44 +0000] [18] [INFO] Waiting for application startup.
[2019-05-24 11:51:45 +0000] [23] [INFO] Booting worker with pid: 23
WARNING:root:email-validator not installed, email fields will be treated as str.
To install, run: pip install email-validator
[2019-05-24 11:51:53 +0000] [23] [INFO] Started server process [23]
[2019-05-24 11:51:53 +0000] [23] [INFO] Waiting for application startup.
[2019-05-24 11:51:54 +0000] [28] [INFO] Booting worker with pid: 28
WARNING:root:email-validator not installed, email fields will be treated as str.
To install, run: pip install email-validator
[2019-05-24 11:52:02 +0000] [28] [INFO] Started server process [28]
[2019-05-24 11:52:02 +0000] [28] [INFO] Waiting for application startup.

This will continue untill I shut it down. This problem weirdly does not happen when I run everything locally.

A difference between local and remote deployment is probably only the port that I set. As you can see, I use port 8081:

[INFO] Listening at: http://0.0.0.0:8081 (1)

I set the port by specifying it in my Kubernetes yaml file, but I am unsure if this has something to do with the problem.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:26 (9 by maintainers)

github_iconTop GitHub Comments

2reactions
tiangolocommented, May 24, 2019

WORKERS_PER_CORE is a float telling the container how many workers per core to boot.

If you have it set to 1 and you have 64 processors with 2 cores each one (128 cores), it will start 128 workers.

To make it a fixed number of workers you can use WEB_CONCURRENCY: https://github.com/tiangolo/uvicorn-gunicorn-fastapi-docker#web_concurrency

That will fix the number of workers to that specific number (independent of the number of available cores).

The image is configured to, by default, get the maximum performance available. But it might be a waste of resources and you might not really need to handle 30,000 (or more) requests per second. In that case, you can play around with those configurations as you need.

There are several combinations of configurations you can use, check the docs: https://github.com/tiangolo/uvicorn-gunicorn-fastapi-docker

1reaction
tiangolocommented, Dec 6, 2020

Yeah, I think that could make sense @fdroessler .

Read more comments on GitHub >

github_iconTop Results From Across the Web

Gunicorn: stuck at booting new workers - Stack Overflow
I have a rather simple Flask application (using fastAPI) for loading a numpy array and defining some API endpoints. import numpy as np...
Read more >
Server Workers - Gunicorn with Uvicorn - FastAPI
Here I'll show you how to use Gunicorn with Uvicorn worker processes. ... dead processes and restarting new ones if needed to keep...
Read more >
Settings — Gunicorn 20.1.0 documentation
Settings¶. This is an exhaustive list of settings for Gunicorn. Some settings are only able to be set from a configuration file.
Read more >
"Permission Denied" when trying to deploy FastAPI app using ...
Hello, I have been trying to deploy a FastAPI app to Render using uvicorn but I'm constantly running into errors. When I first...
Read more >
Deploy FastAPI with Hypercorn HTTP/2 ASGI | by Ng Wai Foong
This articles covers FastAPI server deployment for HTTP/2 using another ... Hypercorn can utilise asyncio, uvloop, or trio worker types.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found