Example of how to handle signals of termination with a custom Gunicorn worker.
This is a repository was made as reference for the article in my blog:
- Build with
docker-compose build
- Start the
API
withdocker-compose up
You can run Gunicorn
locally, just make sure that:
- Use a recent
Python
, it was tested with3.10
- Create a virtual environment
- Install the
requirements.txt
- Run
gunicorn
command- Check
Dockerfile
for an example of how to callgunicorn
- Check
- You will have to send Gunicorn a signal manually like:
kill -TERM <Gunicorn PID>
The gunicorn PID can be obtained with
ps aux | grep gunicorn
You can run service_a_client.py
(which depends on requests
) or service_a_client.sh
(which depends on cURL
)
- Run the client application (It will hang on)
- Cancel the
docker-compose
withCTRL+C
for gracefully termination (or killgunicorn
like mentioned above) - Check that the client received a HTTP 200 and a custom reason from the
API
Use the -k
parameter and point to the worker path.module.class
.
Example:
gunicorn -b 0.0.0.0:8080 --timeout 7200 -w 6 -k service_b.worker.CustomWorker service_b.app:app