pythongunicorninstrumentation

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


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?


Solution

  • 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.