これは私が作成している関数です。プロセスの一部が1時間かかるので、リアルタイムで印刷するかどうかを示す1つの引数があります。私は同時にいくつかの副作用をしているので、エラーを発生させてすべてを止めるか、メインスクリプトを実行させるという別の引数があります。python popenを使って印刷したり、通信や戻りコードを保存する方法は?
もし私がprint_real_time Trueであれば、p.communicate()が緩んでいます iterからのすべてのプリントを変数に格納して返すことができましたが、どのようにしてstd outとstderr失敗したかどうかを確認する戻り値を取得しますか?あなたの問題へ
def launch_subprocess_cmd(command_to_lunch, cwd=None, print_real_time=False, raise_errors=True):
"""
for a given command line will lunch that as a subprocess
:param command_to_lunch: string
:param print_real_time: boolean
:param cwd: the folder path from where the command should be run.
:param raise_errors: boolean if the return code of the subprocess is different than 0 raise an error an stop all scripts.
else the main script will keep running and can access the third return value of this function and decide what to do with it.
:return: list com return the stdout and the stderr of the Popen subprocess.
"""
if cwd is None:
p = subprocess.Popen(command_to_lunch, stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=True)
else:
p = subprocess.Popen(command_to_lunch, cwd=cwd, stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=True)
if print_real_time is True:
for i in iter(p.stdout.readline, b''):
print i
com = p.communicate()
if raise_errors is True:
if p.returncode != 0:
raise ValueError("\n\nSubprocess fail: \n" + "Error captures: \n" + "stdout:\n" + com[0] + "\nstderr:\n" + com[1] + "\n")
# com[0] is std_out, com[1] is std_err and p.return code is if the subprocess was successful or not with a int number
return com[0], com[1], p.returncode
おかげでみんな=)
このようなリストにもエラーがあります – pelos