Search code examples
pythongunicorn

How to change number of workers and threads in running process for gunicorn


I want to test performance of WEB service that running inside AWS ECS service depending on number of gunicorn workers.

Entrypoint of the container is:

WORKERS=15
THREADS=15

gunicorn \
  --reload \
  --workers "${WORKERS}" \
  --threads "${THREADS}" \
  --max-requests 10000 \
  --max-requests-jitter 200 \
  --timeout 60 \
  --access-logfile - \
  --error-logfile - \
  --bind 0.0.0.0:8000 \
  config.wsgi:application

The problem:

If I want to change number of workers / threads I have to stop gunicorn → update ECS Task definition (set new number of WORKERS and THREADS) → restart ECS container. It takes too much time if I want to test tens of configurations.

Possible workaround:

It is possible to set mock endless entrypoint like watch -n 1000 "ls -l" and login to ECS container with bash and run gunicorn with desired parameters manually. But it is little bit inconvenient and suppose to create test specific environment. So, I want to avoid this method.

The question:

Is it possible to change number of workers and threads of already running gunicorn instance? To be able test different configurations without rerunning container and stopping its entrypoint process.


Solution

  • You could use config file and reload the config by sending HUP signal to the gunicorn master process. See: reload configuration

    Here is a simple example:

    # Dockerfile
    FROM python:3.9-slim
    
    COPY . /app
    RUN pip install --no-cache-dir -r /app/requirements.txt
    WORKDIR /app
    ENTRYPOINT ["gunicorn",  "app:app"]
    
    # tree ./
    .
    ├── app.py
    ├── Dockerfile
    ├── gunicorn.conf.py
    └── requirements.txt
    
    # cat gunicorn.conf.py
    workers = 2
    threads = 2
    loglevel = 'debug'
    errorlog = '-'
    accesslog = '-'
    pidfile = '/var/run/app.pid'
    
    # reload the configuration by
    kill -HUP $(cat /var/run/app.pid)