這是我創建的函數,我有一個參數可以指示打印實時或不打印,因爲某些過程需要一個小時。因爲我在同一時間分幾個子進程,另一個提出錯誤並停止一切的參數,或者讓主腳本運行。如何使用python popen打印並存儲通信和返回代碼?
,但如果我這樣做print_real_time真的,我鬆p.communicate() 我可以一切從ITER的打印存儲在一個變量和返回,但我怎麼整理性病出來,和標準錯誤,並得到返回值,看看是否失敗或不?
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
謝謝你們=)
aja我們把錯誤也放在這樣的列表中 – pelos