subprocess question re waiting

L

loial

I want to call a child process to run a shell script and wait for that script to finish. Will the code below wait for the script to finish? If not then how do I make it wait?

Any help appreciated.


import subprocess

command = "/home/john/myscript"

process = subprocess.Popen(command, stdin=subprocess.PIPE,stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True, shell=True)

out, err = process.communicate()
returncode = process.returncode
 
A

Alain Ketterlin

loial said:
I want to call a child process to run a shell script and wait for that
script to finish. Will the code below wait for the script to finish?
If not then how do I make it wait? [...]
process = subprocess.Popen(command, stdin=subprocess.PIPE,stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True, shell=True)

process.wait()

-- Alain.
 
D

Dylan Evans

loial said:
I want to call a child process to run a shell script and wait for that
script to finish. Will the code below wait for the script to finish?
If not then how do I make it wait? [...]
process = subprocess.Popen(command,
stdin=subprocess.PIPE,stdout=subprocess.PIPE, stderr=subprocess.PIPE,
close_fds=True, shell=True)

process.wait()

Or use subprocess.call instead which does what you want.
 
D

Dave Angel

loial said:
I want to call a child process to run a shell script and wait for that
script to finish. Will the code below wait for the script to finish?
If not then how do I make it wait? [...]
process = subprocess.Popen(command,
stdin=subprocess.PIPE,stdout=subprocess.PIPE, stderr=subprocess.PIPE,
close_fds=True, shell=True)

process.wait()

Or use subprocess.call instead which does what you want.

-- Alain.

http://docs.python.org/2/library/subprocess.html#popen-objects

or use communicate(), which is what the OP had in the first place.
 
D

Dave Angel

I want to call a child process to run a shell script and wait for that script to finish. Will the code below wait for the script to finish? If not then how do I make it wait?

Any help appreciated.


import subprocess

command = "/home/john/myscript"

process = subprocess.Popen(command, stdin=subprocess.PIPE,stdout=subprocess.PIPE, stderr=subprocess.PIPE, close_fds=True, shell=True)

out, err = process.communicate()
returncode = process.returncode

Yes, communicate() will block until the child process is complete.

http://docs.python.org/2/library/subprocess.html#popen-objects

Note the phrase: "Wait for process to terminate." That's referring to
the shell in your case.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top