Skip to content

Instantly share code, notes, and snippets.

@eungju
Last active March 6, 2017 10:28
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eungju/8499292 to your computer and use it in GitHub Desktop.
Save eungju/8499292 to your computer and use it in GitHub Desktop.
파이썬에서 데드락 해결하기 위해 쓰레드 덤프 얻는 방법.

데드락이 걸렸는데 코드를 봐도 어디인지 모를 때가 있다. 자바는 JVM이 쓰레드 덤프 기능을 제공해서 어떤 쓰레드가 어디서 멈춰 있는지 확인하기 쉬웠는데 파이썬은 쓰레드 덤프를 얻으려면 약간의 코딩이 필요하다.

아래와 같이 모든 쓰레드의 스택 트레이스를 볼 수 있다.

for ident, stack in sys._current_frames().items():
    logger.info(("%d" % ident) + "".join(traceback.format_list(traceback.extract_stack(stack))))

어떤 쓰레드가 멈춰 있는지 안다면 그 쓰레드의 스택 트레이스만 볼 수 있다.

for ident, stack in sys._current_frames().items():
    if ident == t.ident:
        logger.info(("%d" % ident) + "".join(traceback.format_list(traceback.extract_stack(stack))))

의심가는 지점에 위 코드를 넣거나, 어디 인지 전혀 감이 잡히지 않으면 시그널 핸들러 등으로 원할 때 실행해서 스택 트레이스를 보면 어디서 멈춰있는지 확인 할 수 있다.

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