Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

multiprocessing.cpu_count() gives incorrect number of available cores when used within a docker container #43

Closed
KaneRodriguez opened this issue May 20, 2020 · 2 comments
Labels

Comments

@KaneRodriguez
Copy link

As discussed here:

If your container has been allocated (say) a single core and 512 MB of RAM on a machine with 64 cores, multiprocessing.cpu_count() thinks it has 64 cores instead of just 1, and you end up spawning 129 workers. This is way more than the container has resources for, and ends up causing out of memory crashes.

I ran into this issue with too many workers being spawned in a docker container on kubernetes, and my application would be in a constant CrashLoopBackoff state.

Debug logs

{"loglevel": "info", "workers": 64, "bind": "0.0.0.0:80", "graceful_timeout": 120, "timeout": 120, "keepalive": 5, "errorlog": "-", "accesslog": "-", "workers_per_core": 1.0, "use_max_workers": null, "host": "0.0.0.0", "port": "80"}

Docker Image

FROM tiangolo/uvicorn-gunicorn-fastapi:python3.7

Relevant Line of Code

cores = multiprocessing.cpu_count()

Solution

I've modified the default gunicorn_config.py to assign cores to an optional AVAILABLE_CORES variable; This is set when I define the number of cpus for the container.

cores = os.getenv("AVAILABLE_CORES", multiprocessing.cpu_count())
@tiangolo
Copy link
Owner

tiangolo commented Jun 6, 2020

Great! I think you solved your problem, right?

If that's correct, then you can close this issue ✔️

@github-actions
Copy link

Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants