Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python's Subprocess.Popen With Shell=True. Wait till it is completed

Submitting a complex cmd string made of a full file path to an executable, the multiple flags, arguments, parameters, inputs and outputs seems to require me to set shell=True otherwise subprocess.Popen is not able understand anything more complex than just a simple path to executable (with no spaces in a filepath).

In my example I have quite a long cmd:

cmd = " '/Application/MyApp.app/Contents/MacOS/my_executable' '/Path/to/input/files' -some -flags -here -could -be -a -lot '/full/path/to/output/files' "

Submitting this cmd to subprocess.Popen " results to an error that complains on something about the path and not being able to find it.

So instead of using :

proc = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)

check_call seems workings quite well:

proc = subprocess.check_call(cmd, shell=True)

Interesting, only after shell is set to True

shell=True 

the subprocess.check_call works with a supplied cmd.

The side effect is that the rest of the code seems proceeds running without waiting for subprocess.check_call(cmd, shell=True) to finish first.

The code is designed the way that the rest of the execution is dependent on a result of subprocess.check_call(cmd, shell=True).

I wonder if there is anyway to enforce the code execution to wait till subprocess.check_call(cmd, shell=True) is finished. Thanks in advance!

like image 936
alphanumeric Avatar asked Dec 08 '13 07:12

alphanumeric


People also ask

Does Popen wait for process to finish?

Using Popen MethodThe Popen method does not wait to complete the process to return a value.

Does Python wait for subprocess run to finish?

Most of your interaction with the Python subprocess module will be via the run() function. This blocking function will start a process and wait until the new process exits before moving on.

Does Popen have a timeout?

The timeout argument is passed to Popen.communicate() . If the timeout expires, the child process will be killed and waited for. The TimeoutExpired exception will be re-raised after the child process has terminated.

What does shell true mean in subprocess?

callProcess = subprocess.Popen(['ls', '-l']) # without shell. Both work. After reading the docs, I came to know that shell=True means executing the code through the shell. So that means in absence, the process is directly started.


3 Answers

As @mikkas suggest just use it as a list here is a working example:

mainProcess = subprocess.Popen(['python', pyfile, param1, param2], stdout=subprocess.PIPE, stderr=subprocess.PIPE)

# get the return value from the method
communicateRes = mainProcess.communicate()

stdOutValue, stdErrValue = communicateRes

You are calling python.exe pyfile param1 param2

By using communicate() you can get the stdout and stderr as a Tuple

You can use python method split() to split your string to a list for example:

cmd = "python.exe myfile.py arg1 arg2"

cmd.split(" ")

Output:

['python.exe', 'myfile.py', 'arg1', 'arg2']
like image 194
Kobi K Avatar answered Nov 05 '22 06:11

Kobi K


I think the check_call function should wait for the command to finish.

See the docs here http://docs.python.org/2/library/subprocess.html

like image 31
Aaron Avatar answered Nov 05 '22 06:11

Aaron


Check call does not wait. You need to do a process.wait() and check the return code explicitly to get the functionaly you want.

Process = subprocess.Popen('%s' %command_string,stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
            Process.wait()
            if Process1.returncode!=0:
                    print Process1.returncode
                    sendMail()
                    return
            else:
                    sendMail()
like image 22
Sameer Avatar answered Nov 05 '22 07:11

Sameer