Subprocess 1
Subprocess 1
com/2/subprocess/
PyMOTW
Home subprocess – Work with Page Contents
1 of 14 5/21/2020, 11:14 PM
subprocess – Work with additional processes - Python Module of the Week https://pymotw.com/2/subprocess/
import subprocess
# Simple command
subprocess.call(['ls', '-1'],
$ python subprocess_os_system.py
__init__.py
index.rst
interaction.py
repeater.py Now available for Python 3!
signal_child.py
signal_parent.py
subprocess_check_call.py
subprocess_check_output.py
subprocess_check_output_error.py
subprocess_check_output_error_trap_output.py
subprocess_os_system.py
subprocess_pipes.py
subprocess_popen2.py
subprocess_popen3.py
subprocess_popen4.py
subprocess_popen_read.py
subprocess_popen_write.py
subprocess_shell_variables.py
subprocess_signal_parent_shell.py
subprocess_signal_setsid.py
import subprocess
$ python subprocess_shell_variables.py
/Users/dhellmann
Error Handling
2 of 14 5/21/2020, 11:14 PM
subprocess – Work with additional processes - Python Module of the Week https://pymotw.com/2/subprocess/
import subprocess
subprocess.check_call(['false'
$ python subprocess_check_call.py
Capturing Output
import subprocess
output = subprocess.check_output
print 'Have %d bytes in output'
print output
$ python subprocess_check_output.py
3 of 14 5/21/2020, 11:14 PM
subprocess – Work with additional processes - Python Module of the Week https://pymotw.com/2/subprocess/
subprocess_check_output_error.py
subprocess_check_output_error_trap_output.py
subprocess_os_system.py
subprocess_pipes.py
subprocess_popen2.py
subprocess_popen3.py
subprocess_popen4.py
subprocess_popen_read.py
subprocess_popen_write.py
subprocess_shell_variables.py
subprocess_signal_parent_shell.py
subprocess_signal_setsid.py
import subprocess
output = subprocess.check_output
'echo to stdout; echo to stderr 1>&2; exit
shell=True,
)
print 'Have %d bytes in output'
print output
$ python subprocess_check_output_error.py
to stderr
Traceback (most recent call last):
File "subprocess_check_output_error.py", lin
shell=True,
File "/Library/Frameworks/Python.framework/V
7/subprocess.py", line 544, in check_output
raise CalledProcessError(retcode, cmd, out
subprocess.CalledProcessError: Command 'echo t
1>&2; exit 1' returned non-zero exit status 1
import subprocess
output = subprocess.check_output
'echo to stdout; echo to stderr 1>&2; exit
shell=True,
stderr=subprocess.STDOUT,
)
print 'Have %d bytes in output'
print output
4 of 14 5/21/2020, 11:14 PM
subprocess – Work with additional processes - Python Module of the Week https://pymotw.com/2/subprocess/
$ python subprocess_check_output_error_trap_ou
popen
import subprocess
print '\nread:'
proc = subprocess.Popen(['echo'
stdout
)
stdout_value = proc.communicate
print '\tstdout:', repr(stdout_value
$ python subprocess_popen_read.py
read:
stdout: '"to stdout"\n'
import subprocess
print '\nwrite:'
proc = subprocess.Popen(['cat'
stdin
)
5 of 14 5/21/2020, 11:14 PM
subprocess – Work with additional processes - Python Module of the Week https://pymotw.com/2/subprocess/
proc.communicate('\tstdin: to stdin
$ python -u subprocess_popen_write.py
write:
stdin: to stdin
popen2
import subprocess
print '\npopen2:'
proc = subprocess.Popen(['cat'
stdin
stdout
)
stdout_value = proc.communicate
print '\tpass through:', repr
$ python -u subprocess_popen2.py
popen2:
pass through: 'through stdin to stdout
popen3
import subprocess
print '\npopen3:'
proc = subprocess.Popen('cat -; echo "to stder
shell
stdin
stdout
stderr
)
stdout_value, stderr_value =
print '\tpass through:', repr
print '\tstderr :', repr
6 of 14 5/21/2020, 11:14 PM
subprocess – Work with additional processes - Python Module of the Week https://pymotw.com/2/subprocess/
$ python -u subprocess_popen3.py
popen3:
pass through: 'through stdin to stdout
stderr : 'to stderr\n'
popen4
import subprocess
print '\npopen4:'
proc = subprocess.Popen('cat -; echo "to stder
shell
stdin
stdout
stderr
)
stdout_value, stderr_value =
print '\tcombined output:', repr
print '\tstderr value :', repr
$ python -u subprocess_popen4.py
popen4:
combined output: 'through stdin to std
stderr value : None
Connecting Segments of a
Pipe
Multiple commands can be connected into a
pipeline, similar to the way the Unix shell
works, by creating separate Popen instances
and chaining their inputs and outputs
together. The stdout attribute of one Popen
instance is used as the stdin argument for
the next in the pipeline, instead of the
constant PIPE . The output is read from the
stdout handle for the final command in the
pipeline.
import subprocess
cat = subprocess.Popen(['cat'
7 of 14 5/21/2020, 11:14 PM
subprocess – Work with additional processes - Python Module of the Week https://pymotw.com/2/subprocess/
stdout
)
grep = subprocess.Popen(['grep'
stdin
stdout
)
cut = subprocess.Popen(['cut'
stdin
stdout
)
end_of_pipe = cut.stdout
$ python -u subprocess_pipes.py
Included files:
subprocess_os_system.py
subprocess_shell_variables.py
subprocess_check_call.py
subprocess_check_output.py
subprocess_check_output_error.py
subprocess_check_output_error_trap_out
subprocess_popen_read.py
subprocess_popen_write.py
subprocess_popen2.py
subprocess_popen3.py
subprocess_popen4.py
subprocess_pipes.py
repeater.py
interaction.py
signal_child.py
signal_parent.py
subprocess_signal_parent_shell.py
subprocess_signal_setsid.py
8 of 14 5/21/2020, 11:14 PM
subprocess – Work with additional processes - Python Module of the Week https://pymotw.com/2/subprocess/
import sys
sys.stderr.write('repeater.py: starting
sys.stderr.flush()
while True:
next_line = sys.stdin.readline
if not next_line:
break
sys.stdout.write(next_line
sys.stdout.flush()
sys.stderr.write('repeater.py: exiting
sys.stderr.flush()
import subprocess
print
print 'All output at once:'
proc = subprocess.Popen('python repeater.py'
shell
stdin
stdout
)
for i in range(10):
proc.stdin.write('%d\n' %
output = proc.communicate()[0
print output
9 of 14 5/21/2020, 11:14 PM
subprocess – Work with additional processes - Python Module of the Week https://pymotw.com/2/subprocess/
$ python -u interaction.py
Signaling Between
Processes
The os examples include a demonstration of
signaling between processes using os.fork()
and os.kill(). Since each Popen instance
provides a pid attribute with the process id of
the child process, it is possible to do
something similar with subprocess . For
example, using this script for the child
process to be executed by the parent process
import os
import signal
import time
import sys
pid = os.getpid()
received = False
10 of 14 5/21/2020, 11:14 PM
subprocess – Work with additional processes - Python Module of the Week https://pymotw.com/2/subprocess/
sys.stdout.flush()
if not received:
print 'CHILD %6s: Never received signal'
import os
import signal
import subprocess
import time
import sys
proc = subprocess.Popen(['python'
print 'PARENT : Pausing before sending si
sys.stdout.flush()
time.sleep(1)
print 'PARENT : Signaling child'
sys.stdout.flush()
os.kill(proc.pid, signal.SIGUSR1
$ python signal_parent.py
import os
import signal
import subprocess
import tempfile
import time
import sys
script = '''#!/bin/sh
echo "Shell script in process $$"
set -x
python signal_child.py
'''
script_file = tempfile.NamedTemporaryFile
11 of 14 5/21/2020, 11:14 PM
subprocess – Work with additional processes - Python Module of the Week https://pymotw.com/2/subprocess/
script_file.write(script)
script_file.flush()
proc = subprocess.Popen(['sh'
print 'PARENT : Pausing before sending si
sys.stdout.flush()
time.sleep(1)
print 'PARENT : Signaling child
sys.stdout.flush()
os.kill(proc.pid, signal.SIGUSR1
time.sleep(3)
1. subprocess_signal_parent_shell.py
2. The Unix shell process running the
script created by the main python
program.
3. signal_child.py
$ python subprocess_signal_parent_shell.py
import os
import signal
import subprocess
import tempfile
import time
import sys
12 of 14 5/21/2020, 11:14 PM
subprocess – Work with additional processes - Python Module of the Week https://pymotw.com/2/subprocess/
script = '''#!/bin/sh
echo "Shell script in process $$"
set -x
python signal_child.py
'''
script_file = tempfile.NamedTemporaryFile
script_file.write(script)
script_file.flush()
proc = subprocess.Popen(['sh'
close_fds
preexec_fn
)
print 'PARENT : Pausing before sending si
sys.stdout.flush()
time.sleep(1)
print 'PARENT : Signaling process group
sys.stdout.flush()
os.killpg(proc.pid, signal.SIGUSR1
time.sleep(3)
$ python subprocess_signal_setsid.py
See also:
13 of 14 5/21/2020, 11:14 PM
subprocess – Work with additional processes - Python Module of the Week https://pymotw.com/2/subprocess/
subprocess
Standard library documentation for
this module.
os
pipes
© Copyright Doug Hellmann. | | Last updated on Mar 16, 2019. | Created using Sphinx. | Design based
on "Leaves" by SmallPark |
14 of 14 5/21/2020, 11:14 PM