python子进程由一个by运行

时间:2017-07-17 07:24:08

标签: python python-2.7 python-3.x subprocess

我有一些过程:

subprocess.Popen(['python2.7 script1.py')],shell=True)
subprocess.Popen(['python2.7 script2.py')],shell=True)
subprocess.Popen(['python2.7 script3.py')],shell=True)
subprocess.Popen(['python2.7 script4.py')],shell=True)

我希望每个人在前一个过程完成后开始。 我的意思是

subprocess.Popen(['python2.7 script2.py')],shell=True)

之后开始
subprocess.Popen(['python2.7 script1.py')],shell=True)

完成,并为其他人完成相同。这是因为以前的脚本具有下一个脚本使用的输出。 感谢

2 个答案:

答案 0 :(得分:3)

你可以简单地使用wait()完成每一个,如下所示:

sp1 = subprocess.Popen(['python2.7 script1.py')],shell=True)
sp1.wait()

sp2 = subprocess.Popen(['python2.7 script2.py')],shell=True)
sp2.wait()

sp3 = subprocess.Popen(['python2.7 script3.py')],shell=True)
sp3.wait()

sp4 = subprocess.Popen(['python2.7 script4.py')],shell=True)
sp4.wait()

或者用更短的方式:

subprocess.Popen(['python2.7 script1.py')],shell=True).wait()
subprocess.Popen(['python2.7 script2.py')],shell=True).wait()
subprocess.Popen(['python2.7 script3.py')],shell=True).wait()
subprocess.Popen(['python2.7 script4.py')],shell=True).wait()

答案 1 :(得分:2)

使用subprocess.call

  

运行 args 描述的命令。等待命令完成,然后返回returncode属性。

在你的例子中:

subprocess.call(['python2.7 script1.py')],shell=True)
subprocess.call(['python2.7 script2.py')],shell=True)
subprocess.call(['python2.7 script3.py')],shell=True)
subprocess.call(['python2.7 script4.py')],shell=True)