파이썬 어플리케이션의 stack trace
조회수 2724회
1 답변
-
저도 평소엔 잘 실행되다가 가끔 왠지 모르게 프로그램이 종료되는 경우가 있었는데 그때 제가 썼던 방법을 알려드릴게요. 제가 개인적으로 만든 코드라서 범용은 아니고 unix환경에서만 쓸 수 있습니다.
import code, traceback, signal def debug(sig, frame): """Interrupt running process, and provide a python prompt for interactive debugging.""" d={'_frame':frame} # Allow access to frame object. d.update(frame.f_globals) # Unless shadowed by global d.update(frame.f_locals) i = code.InteractiveConsole(d) message = "Signal received : entering python shell.\nTraceback:\n" message += ''.join(traceback.format_stack(frame)) i.interact(message) def listen(): signal.signal(signal.SIGUSR1, debug) # Register handler
쓸 때는 프로그램 시작 부분에
listen()
함수를 한번 호출해줘야 합니다.os.kill(pid, signal.SIGUSR1)
처럼 kill등을 써서SIGUSR1
시그널을 보내면 파이썬 콘솔이 멈추고 stack trace한 결과를 보여줍니다.stack trace에서 계속 실행하려면
ctrl-d(EOF)
를 눌러주세요.
댓글 입력