subprocess.Popen
でいくつかのプログラムを実行し、ファイルにstdout
/stderr
を記録するpythonサービスがあります。それは答えhereに基づいてうまく動作します。サブプロセスからstdoutをファイルに安全にリダイレクトします.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'が破壊されます。 –