2

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

asked Jul 17, 2017 at 7:24

2 Answers 2

4

Use subprocess.call:

Run the command described by args. Wait for command to complete, then return the returncode attribute.

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)
Tiago Peres
15.9k22 gold badges105 silver badges174 bronze badges
answered Jul 17, 2017 at 7:30
Sign up to request clarification or add additional context in comments.

Comments

4

You can simply use wait() for each one to finish, like this :

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()
london_utku
1,3122 gold badges20 silver badges47 bronze badges
answered Jul 17, 2017 at 7:28

1 Comment

There is a paranthesis mistake here : (['python2.7 script1.py')], which should be corrected.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.