handy way to spot-profile a python program (watch+pstat+tac)

Mike tutufan at gmail.com
Mon Feb 2 18:27:33 EST 2009

Someone (forget who) mentioned recently that you could get some "poor
man's" profiling info by attaching to a running python with gdb, and
periodically grabbing a stack trace.

I figured out that there's a handy way to do this with this command:

    watch -n 1 'pstack 30154 | tac'

which will show a full-screen stack dump once per second on the python
with pid 30154.  (It works with non-Python, too--the 'p' is apparently
for "process", not "python".)

If the interesting part runs off the bottom of your screen, you can
always leave off the tac, though that makes it a bit jumpier.

(pstack uses the ptrace interface, which may annoy the parents of this
process if they're paying attention (which is not usually the case).)

Have fun,

More information about the Python-list mailing list