subprocess.Popen
으로 일부 프로그램을 실행하고 파일에 stdout
/stderr
을 기록하는 python 서비스가 있습니다. 그것은 대답 here을 기반으로 멋지게 작동합니다.stdout을 subprocess.Popen에서 파일로 안전하게 리디렉션합니다.
서비스로 실행 중이므로 정리가 올바르게 수행되었는지 확인하고 싶습니다.
while RunningService:
try:
command = "some command to run the program with arguments"
with open('out-file.txt', 'a') as f_out:
myProc = subprocess.Popen(command, stdout=f_out, stderr=f_out)
# some looping/waiting
# ...
# ...
if breakCondition:
break
except Exception as e:
# do some error logging for the caught exception
finally:
myProc.kill()
은 내가 이해하고 싶은 것은 우리가 프로세스를 종료 할 때 f_out
을 처리하는 파일에 발생하는 것입니다 : 코드는 다음과 유사한 구조를 가지고있다. 파일은 언제 닫습니까? 다른 함수에서 Popen 객체를 생성하고 반환한다면 다른 것입니까?
'with open ... as f_out : '범위를 종료하면'f_out'이 삭제됩니다. –