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
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).
A Popen object has a . wait() method exactly defined for this: to wait for the completion of a given subprocess (and, besides, for retuning its exit status). If you use this method, you'll prevent that the process zombies are lying around for too long. (Alternatively, you can use subprocess.
This wait()method in Python is a method of os module which generally makes the parent process to synchronize with its child process which means the parent will wait for the child process to complete its execution (i.e wait until the exit of the child process) and later continue with its process execution.
The main difference is that subprocess. run() executes a command and waits for it to finish, while with subprocess. Popen you can continue doing your stuff while the process finishes and then just repeatedly call Popen.
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.
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") + " .")
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With