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

Tell Oryx to configure gunicorn workers dynamically #461

Merged
merged 3 commits into from Jul 26, 2023

Conversation

pamelafox
Copy link
Collaborator

@pamelafox pamelafox commented Jul 26, 2023

Purpose

This PR enables PYTHON_ENABLE_GUNICORN_MULTIWORKERS on App Service. When Oryx sees that, it will auto configure gunicorn to use a worker count based off the formula of CPU_COUNT * 2 + 1. This should hopefully resolve issues like #272.

Here's Oryx docs about the config option:
https://github.com/microsoft/Oryx/blob/5239b0416cdddcf5bf0aef6703eb339efa865d67/doc/runtimes/python.md?plain=1#L101

The other approach is for us to specify our own startup script that does the same thing (as I describe in #272 (comment)), but for now, this is the quickest fix.

Does this introduce a breaking change?

[ ] Yes
[X] No

Pull Request Type

What kind of change does this Pull Request introduce?

[ ] Bugfix
[X] Feature
[ ] Code style update (formatting, local variables)
[ ] Refactoring (no functional changes, no api changes)
[ ] Documentation content changes
[ ] Other... Please describe:

How to Test

  • azd up
  • Check Advanced Tools > Download as zip > default_docker.log, see that it spawned 3 workers
  • Also SSH into App Service and check out /opt/startup/startup.sh, you'll see workers=3 in the command.

@pamelafox
Copy link
Collaborator Author

cc @jongio We may want to put this setting by default in core/host/appservice.bicep - the only reason its not default on Oryx is backwards compatibility issues.

@chip-davis
Copy link

Just chiming in that making this change fixed the issue I was having as mentioned in #272

@chuwik
Copy link
Collaborator

chuwik commented Jul 26, 2023

cc @jongio We may want to put this setting by default in core/host/appservice.bicep - the only reason its not default on Oryx is backwards compatibility issues.

+1 to making the change in core/host/appservice.bicep, feels like this should be specific to the app service config rather than in the main bicep file.

@pamelafox pamelafox merged commit 0f5fb26 into Azure-Samples:main Jul 26, 2023
6 checks passed
@pamelafox pamelafox deleted the worker-config branch July 26, 2023 22:01
@vrajroutu
Copy link

vrajroutu commented Jul 26, 2023

@pamelafox Can you also look in to this? Adjust the number of workers based on the number of CPU cores

app/start.sh

#!/bin/sh  
  
echo ""  
echo "Loading azd .env file from current environment"  
echo ""  
  
while IFS='=' read -r key value; do  
    value=$(echo "$value" | sed 's/^"//' | sed 's/"$//')  
    export "$key=$value"  
done <<EOF  
$(azd env get-values)  
EOF  
  
if [ $? -ne 0 ]; then  
    echo "Failed to load environment variables from azd environment"  
    exit $?  
fi  
  
echo 'Creating python virtual environment "backend/backend_env"'  
python -m venv backend/backend_env  
  
echo ""  
echo "Restoring backend python packages"  
echo ""  
  
cd backend  
./backend_env/bin/python -m pip install -r requirements.txt  
if [ $? -ne 0 ]; then  
    echo "Failed to restore backend python packages"  
    exit $?  
fi  
  
echo ""  
echo "Restoring frontend npm packages"  
echo ""  
  
cd ../frontend  
npm install  
if [ $? -ne 0 ]; then  
    echo "Failed to restore frontend npm packages"  
    exit $?  
fi  
  
echo ""  
echo "Building frontend"  
echo ""  
  
npm run build  
if [ $? -ne 0 ]; then  
    echo "Failed to build frontend"  
    exit $?  
fi  
  
echo ""  
echo "Starting backend"  
echo ""  
  
cd ../backend  
xdg-open http://127.0.0.1:8000  
  
# Install gunicorn  
./backend_env/bin/pip install gunicorn  
  
# Adjust the number of workers based on the number of CPU cores  
workers=$(( $(nproc) * 2 ))  
  
# Start the backend using Gunicorn with multiple workers  
./backend_env/bin/gunicorn -w $workers -b 127.0.0.1:8000 app:app  
  
if [ $? -ne 0 ]; then  
    echo "Failed to start backend"  
    exit $?  
fi  

HughRunyan pushed a commit to RMI/RMI_chatbot that referenced this pull request Mar 26, 2024
* Auto-configure optimal workers

* Revert model changes

* Move to appservice.bicep conditional on language
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants