How to understand how workers are being used up in gunicorn

Posted by Arica Deslauriers on Tuesday, May 21, 2024

I basically would like to know how gunicorn workers work. I have a server with 4 workers on a machine, with 4GB RAM, and 2 CPU and having nginx as the frontend serving requests and reverse proxy. I have simultaneous requests being sent to the server.

  • I wish to know how the workers are being used up, if they are four requests, are they load balanced across the four workers as If 1 requests for each 1 worker?

  • Also how to check how much memory is used up for each worker? I have set the max requests to 100. So using this 100 max requests. will it reload the entire 4 workers even if 1 worker has reached 100 requests.

  • How to get more insight of the workers and how the workers memory and no of requests currently in each worker.

  • 1 Answer

    Short answer: Depends on the worker type and gunicorn configuration.

    Long answer:

  • Yes, as long as there are workers available. When gunicorn is started, the -w option configures the number of workers, implementation of which varies depending on worker type. Some worker types use threads, others use event loops and are asynchronous. Performance varies depending on the type - in general async via event loop is preferred as it is lighter on resources and more performant.
  • Each worker is forked from the main gunicorn process. Memory use can be seen with ps thread output, for example ps -fL -p <gunicorn pid>. Max connections is per worker from documentation so only the worker that reaches 100 connections will be reloaded.
  • There is a stats collecting library for gunicorn though I have not used it myself.
  • 2

    ncG1vNJzZmirpJawrLvVnqmfpJ%2Bse6S7zGiorp2jqbawutJoanFxYWyFdYOOoaawZaSkera6w56prKyRo7FutM6wZLCnoqCys7%2BMmqmeZZKatq%2BzjK6qnpxdqr1utc1mnq6mmZi8s7o%3D