Python popen command. Wait until the command is finished Python popen command. Wait until the command is finished python python

Python popen command. Wait until the command is finished


Depending on how you want to work your script you have two options. If you want the commands to block and not do anything while it is executing, you can just use subprocess.call.

#start and block until donesubprocess.call([data["om_points"], ">", diz['d']+"/points.xml"])

If you want to do things while it is executing or feed things into stdin, you can use communicate after the popen call.

#start and process things, then waitp = subprocess.Popen([data["om_points"], ">", diz['d']+"/points.xml"])print "Happens while running"p.communicate() #now wait plus that you can send commands to process

As stated in the documentation, wait can deadlock, so communicate is advisable.


You can you use subprocess to achieve this.

import subprocess#This command could have multiple commands separated by a new line \nsome_command = "export PATH=$PATH://server.sample.mo/app/bin \n customupload abc.txt"p = subprocess.Popen(some_command, stdout=subprocess.PIPE, shell=True)(output, err) = p.communicate()  #This makes the wait possiblep_status = p.wait()#This will give you the output of the command being executedprint "Command output: " + output


Force popen to not continue until all output is read by doing:

os.popen(command).read()