Home

subprocessPIPE

subprocess.PIPE is a sentinel value in the Python standard library's subprocess module used to request the creation of a new pipe to a child process. It connects the parent process to one or more of the child’s standard streams (stdin, stdout, and stderr) so that input can be supplied or output captured programmatically. The constant is defined in the module as -1 and is portable across platforms, including Windows and Unix-like systems.

Common usage involves passing PIPE as the value for one or more of the stream parameters when

A typical pattern is to start a process and read its output using the communicate method, which

Common caveats include the potential for deadlocks if a subprocess produces more data than can be consumed

creating
a
subprocess.
For
example,
one
can
set
stdout=subprocess.PIPE
and/or
stderr=subprocess.PIPE
to
capture
the
child
process’s
output,
or
set
stdin=subprocess.PIPE
to
supply
input
to
the
child.
This
mechanism
is
used
with
functions
such
as
Popen
and
run,
allowing
the
parent
to
interact
with
the
child’s
streams
either
directly
or
via
convenience
methods.
reads
the
streams
until
the
process
terminates.
For
instance,
running
a
command
and
capturing
its
output
can
be
done
with
Popen(['cmd',
'args'],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
followed
by
out,
err
=
proc.communicate().
Alternatively,
subprocess.run
can
be
used
with
stdout=subprocess.PIPE
and
stderr=subprocess.PIPE,
or
with
capture_output=True
in
newer
versions.
promptly.
To
avoid
this,
it
is
recommended
to
read
from
or
pipe
the
process’s
output
promptly
or
use
the
communicate
method.
PIPE
is
intended
for
inter-process
communication
within
a
single
program
and
is
not
the
same
as
shell
piping.