Diagnosing a hung Celery worker or gunicorn process with py-spy

Backend Live Coding November 13, 2026 bash

py-spy is a sampling profiler that reads the process memory from the outside, so it is safe in production.

bash
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
# ======================================================================
# Q: A Celery worker or gunicorn process is stuck at 100% CPU or not responding.
# What do you do?
#
# py-spy is a sampling profiler that reads the process memory from the outside,
# so it is safe in production. The dump immediately shows whether the process is
# waiting on a socket without a timeout (the usual culprit: requests without a
# timeout to the ERP), on a database lock, or in an infinite loop. Once I have
# the stack, I restart as mitigation.
# ======================================================================

# stack of all threads without a restart and without changing code
py-spy dump --pid 12345

# live top-like view: which functions eat CPU
py-spy top --pid 12345

# flamegraph for 30s
py-spy record -o profile.svg --pid 12345 --duration 30

# in a container SYS_PTRACE is required
docker exec -it worker py-spy dump --pid 1