2017-07-17 39 views
1

運行一個我有某種的過程:蟒子由

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) 

完全地結束後,和其他人一樣。這是因爲以前的腳本已經輸出了它被下一個腳本使用的輸出。 感謝

回答

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() 
2

使用subprocess.call

運行命令描述參數。等待命令完成,然後返回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) 
+0

感謝您的編輯,奧弗! –

+0

我的榮幸.... –