하위 프로세스에서 실행중인 python 프로그램의 추적에 액세스하려고합니다.하위 프로세스 하위 추적
The documentation는 말한다 : my_sub_program.py
의
Exceptions raised in the child process, before the new program has started to execute, will be re-raised in the parent. Additionally, the exception object will have one extra attribute called child_traceback, which is a string containing traceback information from the child’s point of view.
내용 : my_main_program.py
의
raise Exception("I am raised!")
내용 : 나는 my_main_program.py
을 실행하면, 나는 다음과 같은 오류가
import sys
import subprocess
try:
subprocess.check_output([sys.executable, "my_sub_program.py"])
except Exception as e:
print e.child_traceback
:
Traceback (most recent call last):
File "my_main_program.py", line 6, in <module>
print e.child_traceback
AttributeError: 'CalledProcessError' object has no attribute 'child_traceback'
어떻게하면 서브 프로세스 프로그램 코드를 수정하지 않고 서브 프로세스의 추적을 액세스 할 수 있습니까? 즉, 내 전체 서브 프로그램 코드 주위에 큰 try/except
절을 추가하는 것을 피하고 싶지만 내 메인 프로그램에서 오류 로깅을 처리해야합니다.
편집 :sys.executable
은 주 프로그램을 실행하는 인터프리터와 다른 인터프리터로 교체 가능해야합니다.
Doc는 "새 프로그램이 실행되기 전에"라고 말합니다. 새 프로그램이 실행되는 동안 예외가 발생 했으므로 'child_traceback'이 없습니다. 일단 새로운 프로그램이 실행되면'CalledProcessError' 예외를 잡아서 다음과 같이해야합니다 : http://stackoverflow.com/questions/24849998/how-to-catch-exception-output-from-python-subprocess-check -output 'CalledProcessError.output'을 사용하여 출력 – mguijarr
예에서'CalledProcessError.output'은 표준 출력만을 캡처했지만 Exception의 추적은 포착하지 않았습니다. – schreon
이것은 출력이'stderr'에서 보내 졌기 때문일 것입니다. 위의 링크를 보내 주신 질문의 답변을 참조하십시오. – mguijarr