Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 718
jameshwart lopez Avatar asked Mar 12 '18 02:03

jameshwart lopez


People also ask

Does subprocess run wait for finish?

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).

How do you wait for subprocess to Popen to finish Python?

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.

How do you wait for a function to finish in Python?

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.

What is the difference between subprocess Popen and run?

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.


3 Answers

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.

like image 186
Marius Avatar answered Oct 15 '22 08:10

Marius


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") + " .")
like image 21
Jyoti Kataria Avatar answered Oct 15 '22 09:10

Jyoti Kataria


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.

like image 31
Rafeed Avatar answered Oct 15 '22 08:10

Rafeed