Skip to content

Instantly share code, notes, and snippets.

@Kylep342
Last active March 10, 2022 22:31
Show Gist options
  • Save Kylep342/4edb39948cacb99f4a37c6c81f1f830e to your computer and use it in GitHub Desktop.
Save Kylep342/4edb39948cacb99f4a37c6c81f1f830e to your computer and use it in GitHub Desktop.
Dump the stack traces of all executing Python threads when the Python Process receives the `usr1` signal.
import signal
import sys
import traceback
def sigterm_handler(signal, frame):
# save the state here or do whatever you want
for thread_id, frame in sys._current_frames().items():
logger.info(f"Stack for thread {thread_id}")
traceback.print_stack(frame, file=sys.stdout)
logger.info("")
signal.signal(signal.SIGUSR1, sigterm_handler)
@Kylep342
Copy link
Author

Kylep342 commented Mar 10, 2022

Example:

kill -usr1 $PID

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment