'Wait subprocess.run until completes its task
I have created a simple method that executes a command like you do in the terminal
from subprocess import PIPE, run
class Command_Line():
@staticmethod
def execute(command):
result = run(command, stdout=PIPE, stderr=PIPE, universal_newlines=True, shell=True)
print(result)
return result.stdout
My problem with the code above is it does not wait until the task/process is done. Lets say i use ffmpeg to change the frame rate of a video via the following code
import Command_Line as cmd
cmd.execute('ffmpeg -i "000000004.avi" -c copy -y -r 30 "000000004.avi"')
The problem is the output video because it does not complete the process. I've search how to wait like Is there a way to check if a subprocess is still running? but could not incorporate it with my code. Can you share your experience with this.
Thanks
Solution 1:[1]
According to the python documentation subprocess.run
waits for the process to end.
The problem is that ffmpeg
overwrites the input file if the input and output files are the same and therefore the output video becomes unusable.
Solution 2:[2]
For me subprocess.run()
isn't waiting for the first command to be completed. It's executing the second command as well!
Hence, it's not able to find the file, which is the result of the first command (and is the input for the second).
subprocess.run("sh /MGMUST1/SHARED/RESOURCES/CODE/TRANSCRIPTOME/ONCOPEPT/ONCOPEPT_INDIA/Paired_end.QC.sh" + "; cp " + os.path.join("/MGMUST1/SHARED/RESOURCES/CODE/DEMUX-Scripts/QC-RESULTS/", str(pid) +"/"+ str(eid)+"/"+ str(pid)+"_"+str(eid)+".pdf") + " .")
Solution 3:[3]
subprocess.run() is synchronous which means that the system will wait till it finishes before moving on to the next command. subprocess.Popen() does the same thing but it is asynchronous (the system will not wait for it to finish). You can try reloading your file using importlib.reload command. It may find your generated file then.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Marius |
Solution 2 | Zearin |
Solution 3 | Rafeed |