Any way to read number of active workers of gunicorn managed by the arbiter from command line?

Viewed 1839

As explained here Gunicorn provides an optional instrumentation of the arbiter and workers using the statsD protocol over UDP.

My question: Is there any way to read number of active (i.e. processing some request) gunicorn workers in realtime from command line without installing statsD? My server load average sometimes peaks and I want to see how many gunicorn workers are busy in that time?

2 Answers

Gunicorn's default statsd tracking doesn't actually track active vs nonactive workers. It just tells you the total number of workers, which is not nearly as useful.

But Gunicorn does provide server hooks to let you run code at various points in the process. This is the solution I came up with:

sc = statsd.StatsClient(domain, port, prefix=statsd_prefix)

def pre_request(worker, req):
    # Increment busy workers count
    sc.incr('busy_workers', 1)

def post_request(worker, req, environ, resp):
    # Decrement busy workers count
    sc.decr('busy_workers', 1)

You gotta put that in your config file and then reference the config file when you start up gunicorn.

gunicorn myapp.wsgi:application --config myapp/gunicorn_config.py

If you don't want to use statsd you could use those same triggers but pipe a signal to any other program that can keep a count and then watch it to see it on the commandline.

Not sure if there is any particular command for it.

But generally it can be done using shell. Number of workers mean number of processes. Hence you can simple check all the active process, then find all the process which has gunicorn in it and then count all such entries. Do remember to exclude the grep search because it is also has gunicorn.

You command would look something like this.

ps aux | grep gunicorn | grep -v grep | wc -l

Related