Skip to content

Instantly share code, notes, and snippets.

@jaysonsantos
Last active September 17, 2015 12:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaysonsantos/4838023f1d07c755cb1f to your computer and use it in GitHub Desktop.
Save jaysonsantos/4838023f1d07c755cb1f to your computer and use it in GitHub Desktop.
Mimic java and dump all threads stacktrace when CRTL + \ is pressed
import functools
import signal
import sys
import time
import traceback
import threading
lock = threading.Lock()
def _dump_data():
with lock:
sys_err = open('/dev/stderr', 'w')
log = functools.partial(print, file=sys_err, flush=True)
threads = {thread.ident: thread for thread in threading.enumerate()}
for ident, frame in sys._current_frames().items():
log('=' * 80)
log('Thead', threads[ident].name, ident)
log(''.join(traceback.format_stack(frame)))
log('=' * 80, '\n\n\n')
def dump_data(*args, **kwargs):
threading.Thread(target=_dump_data).start()
signal.signal(signal.SIGQUIT, dump_data)
while True:
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment