할당 프로세스를 시작하고 stdout
및 stderr
을 검색하려고합니다. 실시간으로 처리하는 데 정말로 신경 쓰지 않습니다.stderr의 첫 번째 행만 얻을 수 있습니다
subprocess.check_ouput()
, 그러나을 사용하고 싶었지만 프로세스가 실패 할 수 있습니다. 에 StackOverflow 및 파이썬 문서를 읽은 후 나는 try .. catch
블록 추가 :
def execute(cmd,timeinsec=60):
print("execute ",cmd, " with time out ",timeinsec)
try:
output = subprocess.check_output(cmd, stderr=subprocess.STDOUT, timeout=timeinsec,universal_newlines=True)
except subprocess.TimeoutExpired:
print("timeout expired")
return "",2
except subprocess.CalledProcessError:
print("called process failed")
return "",1
print ('The command returned the following back to python:'+output)
return output,0
을하지만 output.decode('utf-8')
로 출력을 인쇄 할 때 난 그냥 출력의 첫 번째 라인을 얻을.
참고 : Windows의 Msysgit 1.8과 함께 배포되는 MSys 환경에서이 프로그램을 실행하고 있습니다.
무엇이 잘못 될 수 있는지 알고 계십니까? 이 작업을 수행하는 더 좋은 방법을 알고 계십니까?
[SSCCE] (http://SSCCE.org) –