Python popen command. Wait until the command is finished

PythonSubprocessWaitPopen

Python Problem Overview


I have a script where I launch with popen a shell command. The problem is that the script doesn't wait until that popen command is finished and go continues right away.

om_points = os.popen(command, "w")
.....

How can I tell to my Python script to wait until the shell command has finished?

Python Solutions


Solution 1 - Python

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 done
subprocess.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 wait
p = 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.

Solution 2 - Python

You can you use subprocess to achieve this.

import subprocess

#This command could have multiple commands separated by a new line \n
some_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 possible
p_status = p.wait()

#This will give you the output of the command being executed
print "Command output: " + output

Solution 3 - Python

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

os.popen(command).read()

Solution 4 - Python

Let the command you are trying to pass be

os.system('x')

then you covert it to a statement

t = os.system('x')

now the python will be waiting for the output from the commandline so that it could be assigned to the variable t.

Solution 5 - Python

What you are looking for is the wait method.

Solution 6 - Python

wait() works fine for me. The subprocesses p1, p2 and p3 are executed at the same. Therefore, all processes are done after 3 seconds.

import subprocess

processes = []

p1 = subprocess.Popen("sleep 3", stdout=subprocess.PIPE, shell=True)
p2 = subprocess.Popen("sleep 3", stdout=subprocess.PIPE, shell=True)
p3 = subprocess.Popen("sleep 3", stdout=subprocess.PIPE, shell=True)

processes.append(p1)
processes.append(p2)
processes.append(p3)

for p in processes:
    if p.wait() != 0:
        print("There was an error")

print("all processed finished")

Solution 7 - Python

I think process.communicate() would be suitable for output having small size. For larger output it would not be the best approach.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmicheleView Question on Stackoverflow
Solution 1 - PythonunholysamplerView Answer on Stackoverflow
Solution 2 - PythonTouchstoneView Answer on Stackoverflow
Solution 3 - PythonAlbertoView Answer on Stackoverflow
Solution 4 - PythonChidhambararajan NRMView Answer on Stackoverflow
Solution 5 - PythonOlivier VerdierView Answer on Stackoverflow
Solution 6 - PythonsimibacView Answer on Stackoverflow
Solution 7 - PythonBarsha LamichhaneView Answer on Stackoverflow