シグナル(SIGUSR1)を送りつけたタイミングで、どこが実行されているのか アクティブなスレッド全部のスタックトレースを表示する。

import signal, sys, threading, traceback

def dump_stacks(signum, tb):
    print("----------------------------------------")
    for th in threading.enumerate():
        print(th)
        traceback.print_stack(sys._current_frames()[th.ident], limit=None)

signal.signal(signal.SIGUSR1, dump_stacks)

シグナルは普通にkillコマンドで。

kill -SIGUSR1 {pid}

こんな感じに表示される。

----------------------------------------
<_MainThread(MainThread, started 140320591279936)>
  File "app.py", line 19, in <module>
    app.run(debug=True)
  File "/home/makki/Projects/test/venv/lib/python2.7/site-packages/flask/app.py", line 841, in run
    run_simple(host, port, self, **options)
  File "/home/makki/Projects/test/venv/lib/python2.7/site-packages/werkzeug/serving.py", line 812, in run_simple
    reloader_type)
  File "/home/makki/Projects/test/venv/lib/python2.7/site-packages/werkzeug/_reloader.py", line 273, in run_with_reloader
    reloader.run()
  File "/home/makki/Projects/test/venv/lib/python2.7/site-packages/werkzeug/_reloader.py", line 164, in run
    self._sleep(self.interval)
  File "app.py", line 8, in dump_stacks
    traceback.print_stack(sys._current_frames()[th.ident], limit=None)
<Thread(Thread-1, started daemon 140320488511232)>
  File "/home/makki/.linuxbrew/Cellar/python/2.7.14/lib/python2.7/threading.py", line 774, in __bootstrap
    self.__bootstrap_inner()
  File "/home/makki/.linuxbrew/Cellar/python/2.7.14/lib/python2.7/threading.py", line 801, in __bootstrap_inner
    self.run()
  File "/home/makki/.linuxbrew/Cellar/python/2.7.14/lib/python2.7/threading.py", line 754, in run
    self.__target(*self.__args, **self.__kwargs)
  File "/home/makki/Projects/test/venv/lib/python2.7/site-packages/werkzeug/serving.py", line 777, in inner
    srv.serve_forever()
  File "/home/makki/Projects/test/venv/lib/python2.7/site-packages/werkzeug/serving.py", line 612, in serve_forever
    HTTPServer.serve_forever(self)
  File "/home/makki/.linuxbrew/Cellar/python/2.7.14/lib/python2.7/SocketServer.py", line 231, in serve_forever
    poll_interval)
  File "/home/makki/.linuxbrew/Cellar/python/2.7.14/lib/python2.7/SocketServer.py", line 150, in _eintr_retry
    return func(*args)