Skip to content

Instantly share code, notes, and snippets.

@aivarsk
Last active August 27, 2025 13:41
Show Gist options
  • Select an option

  • Save aivarsk/493e8b8976588185dd685cdb38f98c10 to your computer and use it in GitHub Desktop.

Select an option

Save aivarsk/493e8b8976588185dd685cdb38f98c10 to your computer and use it in GitHub Desktop.
Gunicorn active/busy worker count

Run it in one shell:

gunicorn -c config.py --workers=32 test:app

Test it from another:

while `true`; do curl http://127.0.0.1:8000 & done

The access log in the first shell will print the number of currently active workers like this:

127.0.0.1 - - [27/Aug/2025:16:35:58 +0300] "GET / HTTP/1.1" 200 14 "-" "curl/8.9.1" rt=0.101062 busy=28
127.0.0.1 - - [27/Aug/2025:16:35:58 +0300] "GET / HTTP/1.1" 200 14 "-" "curl/8.9.1" rt=0.100312 busy=25
127.0.0.1 - - [27/Aug/2025:16:35:58 +0300] "GET / HTTP/1.1" 200 14 "-" "curl/8.9.1" rt=0.101495 busy=26
127.0.0.1 - - [27/Aug/2025:16:35:58 +0300] "GET / HTTP/1.1" 200 14 "-" "curl/8.9.1" rt=0.100343 busy=27
127.0.0.1 - - [27/Aug/2025:16:35:58 +0300] "GET / HTTP/1.1" 200 14 "-" "curl/8.9.1" rt=0.103661 busy=25
127.0.0.1 - - [27/Aug/2025:16:35:58 +0300] "GET / HTTP/1.1" 200 14 "-" "curl/8.9.1" rt=0.101265 busy=26
127.0.0.1 - - [27/Aug/2025:16:35:58 +0300] "GET / HTTP/1.1" 200 14 "-" "curl/8.9.1" rt=0.100718 busy=27
127.0.0.1 - - [27/Aug/2025:16:35:58 +0300] "GET / HTTP/1.1" 200 14 "-" "curl/8.9.1" rt=0.101064 busy=27
127.0.0.1 - - [27/Aug/2025:16:35:58 +0300] "GET / HTTP/1.1" 200 14 "-" "curl/8.9.1" rt=0.100302 busy=27
127.0.0.1 - - [27/Aug/2025:16:35:58 +0300] "GET / HTTP/1.1" 200 14 "-" "curl/8.9.1" rt=0.101038 busy=28
127.0.0.1 - - [27/Aug/2025:16:35:58 +0300] "GET / HTTP/1.1" 200 14 "-" "curl/8.9.1" rt=0.100871 busy=29
127.0.0.1 - - [27/Aug/2025:16:35:58 +0300] "GET / HTTP/1.1" 200 14 "-" "curl/8.9.1" rt=0.101149 busy=28
127.0.0.1 - - [27/Aug/2025:16:35:58 +0300] "GET / HTTP/1.1" 200 14 "-" "curl/8.9.1" rt=0.101311 busy=30
127.0.0.1 - - [27/Aug/2025:16:35:58 +0300] "GET / HTTP/1.1" 200 14 "-" "curl/8.9.1" rt=0.101993 busy=29
127.0.0.1 - - [27/Aug/2025:16:35:58 +0300] "GET / HTTP/1.1" 200 14 "-" "curl/8.9.1" rt=0.100535 busy=29
127.0.0.1 - - [27/Aug/2025:16:35:58 +0300] "GET / HTTP/1.1" 200 14 "-" "curl/8.9.1" rt=0.101375 busy=29
127.0.0.1 - - [27/Aug/2025:16:35:58 +0300] "GET / HTTP/1.1" 200 14 "-" "curl/8.9.1" rt=0.100342 busy=29
127.0.0.1 - - [27/Aug/2025:16:35:58 +0300] "GET / HTTP/1.1" 200 14 "-" "curl/8.9.1" rt=0.100739 busy=29
from multiprocessing import Semaphore
accesslog = "-"
access_log_format = '%(h)s %(l)s %(u)s %(t)s "%(r)s" %(s)s %(b)s "%(f)s" "%(a)s" rt=%(L)s busy=%({x-busy}i)s'
busy = Semaphore(0)
def pre_request(worker, req):
busy.release()
req.headers.append(("x-busy", str(busy.get_value())))
def post_request(worker, req, environ, resp):
busy.acquire()
import time
def app(environ, start_response):
"""Simplest possible application object"""
data = b"Hello, World!\n"
status = "200 OK"
response_headers = [
("Content-type", "text/plain"),
("Content-Length", str(len(data))),
]
time.sleep(0.1)
start_response(status, response_headers)
return iter([data])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment