python:当 运行 脚本需要更长的时间时,子进程 returns 什么都没有
python: subprocess returns nothing when running scripts which take longer time
初学者遇到 subrpocess
个问题:
当我 运行 两个脚本的数据较少时,下面的行工作正常,大约需要 10-20 分钟,但是如果要处理更大的数据,子进程 returns 一旦两个脚本都没有完成(假设一个小时后)。
此外:通常使用较少的数据,它也会表现异常,即不返回 status_code/going 通过。然后我必须再次 运行 代码片段,它就可以工作了。任何可靠的推理都会有很大帮助!
status_code = subprocess.run(f"python3 scriptA.py {param_1} & python3 scriptB.py {param_2}",
shell=True).returncode
if status_code == 0:
print("subprocess ended ..") # the control does not come here incase of huge files
some_other_file.main(some_param1, some_param2)
我不明白为什么会这样(或者我应该使用不同的方法吗?),非常感谢任何帮助。谢谢!
脚本示例(scriptA 和 scriptB):
def main(param_2):
some_func_with_csv_operations() # not returning anything
more_funcs()
if __name__ == "__main__":
param_2 = sys.argv[1]
main(param_2)
另外,scriptA 或 scriptB 都没有 sys.exit()
编辑:
截图:
尝试在 if
条件之前打印 status_code
,但没有打印任何内容,在终端中,我看到一个光标在闪烁。
enter image description here
另外(寻找任何 python 进程):
使用ps -ax | grep python
显示没有相关信息(附图片)
enter image description here
单独尝试 运行 您的流程,以更好地了解故障发生的位置
running_processes = []
running_processes.append(subprocess.Popen(["python3", "scriptA.py", f"{param_1}"]))
running_processes.append(subprocess.Popen(["python3", "scriptB.py", f"{param_2}"]))
both_succeeded = True
for p in running_processes:
ret = p.wait()
if ret: # anything other than zero will evaluate to True here
both_succeeded = False
cmd = " ".join(p.args)
print(f'command "{cmd}" returned code {ret}'
if both_succeeded:
do_more_things()
上面使用一个列表来保存运行个进程,这样你就可以循环处理它们,避免重复代码。如果你肯定只有两个子进程,你可以选择不使用循环。
process1 = subprocess.Popen(shlex.split(f"python3 scriptA.py {param_1}"))
process2 = subprocess.Popen(shlex.split(f"python3 scriptB.py {param_2}"))
ret1 = process1.wait()
ret2 = process2.wait()
if ret1 == 0 and ret2 == 0: # or "if not ret1 and not ret2:" would also work
do_more_things()
请注意,此示例中的 Popen requires a list of arguments and won't accept a single string, but I've used shlex.split 允许对命令使用单个字符串并获得相同的结果。
初学者遇到 subrpocess
个问题:
当我 运行 两个脚本的数据较少时,下面的行工作正常,大约需要 10-20 分钟,但是如果要处理更大的数据,子进程 returns 一旦两个脚本都没有完成(假设一个小时后)。
此外:通常使用较少的数据,它也会表现异常,即不返回 status_code/going 通过。然后我必须再次 运行 代码片段,它就可以工作了。任何可靠的推理都会有很大帮助!
status_code = subprocess.run(f"python3 scriptA.py {param_1} & python3 scriptB.py {param_2}",
shell=True).returncode
if status_code == 0:
print("subprocess ended ..") # the control does not come here incase of huge files
some_other_file.main(some_param1, some_param2)
我不明白为什么会这样(或者我应该使用不同的方法吗?),非常感谢任何帮助。谢谢!
脚本示例(scriptA 和 scriptB):
def main(param_2):
some_func_with_csv_operations() # not returning anything
more_funcs()
if __name__ == "__main__":
param_2 = sys.argv[1]
main(param_2)
另外,scriptA 或 scriptB 都没有 sys.exit()
编辑:
截图:
尝试在 if
条件之前打印 status_code
,但没有打印任何内容,在终端中,我看到一个光标在闪烁。
enter image description here
另外(寻找任何 python 进程):
使用ps -ax | grep python
显示没有相关信息(附图片)
enter image description here
单独尝试 运行 您的流程,以更好地了解故障发生的位置
running_processes = []
running_processes.append(subprocess.Popen(["python3", "scriptA.py", f"{param_1}"]))
running_processes.append(subprocess.Popen(["python3", "scriptB.py", f"{param_2}"]))
both_succeeded = True
for p in running_processes:
ret = p.wait()
if ret: # anything other than zero will evaluate to True here
both_succeeded = False
cmd = " ".join(p.args)
print(f'command "{cmd}" returned code {ret}'
if both_succeeded:
do_more_things()
上面使用一个列表来保存运行个进程,这样你就可以循环处理它们,避免重复代码。如果你肯定只有两个子进程,你可以选择不使用循环。
process1 = subprocess.Popen(shlex.split(f"python3 scriptA.py {param_1}"))
process2 = subprocess.Popen(shlex.split(f"python3 scriptB.py {param_2}"))
ret1 = process1.wait()
ret2 = process2.wait()
if ret1 == 0 and ret2 == 0: # or "if not ret1 and not ret2:" would also work
do_more_things()
请注意,此示例中的 Popen requires a list of arguments and won't accept a single string, but I've used shlex.split 允许对命令使用单个字符串并获得相同的结果。