简体   繁体   English

python子进程一个一个运行

[英]python subprocess run by one by

i have some sort of processes:我有一些过程:

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)

i want to each one starts after the previous process completely finish.我希望每个人都在前一个过程完全完成后开始。 i mean我的意思是

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

starts after开始于

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

completly finished, and for others the same.完全完成,其他人也一样。 this is cause previous scripts has output that it is used by next script.这是因为以前的脚本有输出,它被下一个脚本使用。 thanks谢谢

You can simply use wait() for each one to finish, like this:您可以简单地为每个完成使用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()

Or in shorter way:或者用更短的方式:

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

Use subprocess.call :使用subprocess.call

Run the command described by args .运行args描述的命令。 Wait for command to complete, then return the returncode attribute.等待命令完成,然后返回returncode属性。

In your example:在你的例子中:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM