Use gunicorn to reduce the impact of memory leaks

Gunicorn is usually used to serve python wsgi apps. It has two special settings to reduce the impact of memory leaks of the wsgi apps.

Update command line arguments

gunicorn --max-requests 2000 --max-requests-jitter 100 app:app

--max-requests 2000 means that a gunicorn worker process will restart after 2000 requests.

The max-requests alone is not enough. When you run multiple gunicorn worker processes, they will possibly restart together. On that moment, no process will handle incoming requests.

To solve the "restarting together" problem, max-requests-jitter is used. --max-requests 2000 --max-requests-jitter 100 means that a gunicorn worker process will restart after 2000 + randint(0, 100) requests. 2000 + randint(0, 100) is a random number between 2000 and 2100.

Update config file

If you prefer to use the config file, try this:

gunicorn -c config.py app:app

The file config.py should contain:

max_requests = 2000
max_requests_jitter = 100
Posted on 2023-09-14