Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
e3a2fbe
Add subprocess.run_pipeline() for command pipe chaining
gpshead Nov 27, 2025
4feb2a8
Add documentation for subprocess.run_pipeline()
gpshead Nov 27, 2025
2a11d4b
Refactor run_pipeline() to use multiplexed I/O
gpshead Nov 28, 2025
2470e14
Add deadlock prevention tests for run_pipeline()
gpshead Nov 28, 2025
e22d1da
Simplify _communicate_streams() to only accept file objects
gpshead Nov 28, 2025
a3e98a7
Improve test_pipeline_large_data_with_stderr to use large stderr
gpshead Nov 28, 2025
3c28ed6
Remove obsolete XXX comment about non-blocking I/O
gpshead Nov 29, 2025
9f53a8e
Refactor POSIX communicate I/O into shared _communicate_io_posix()
gpshead Nov 29, 2025
d420f29
Fix _communicate_streams_windows to avoid blocking with large input
gpshead Nov 29, 2025
df8f082
Fix memoryview and closed stdin handling in _communicate_streams_posix
gpshead Nov 29, 2025
978cd76
Factor out _flush_stdin() and _make_input_view() helpers
gpshead Nov 29, 2025
15f8a93
Support universal_newlines and use _translate_newlines in run_pipeline
gpshead Nov 29, 2025
e6a78a9
Merge remote-tracking branch 'origin/main' into claude/subprocess-pip…
gpshead Apr 25, 2026
5bc5be4
gh-47798: Fix run_pipeline text-mode breaking the Windows backend
gpshead Apr 25, 2026
ae3ee12
gh-47798: Fix TimeoutExpired.output picking wrong stream in run_pipeline
gpshead Apr 25, 2026
11ada45
gh-47798: Reject stdin=PIPE in run_pipeline()
gpshead Apr 25, 2026
51e1ae2
gh-47798: Reject close_fds=False in run_pipeline()
gpshead Apr 25, 2026
c218959
gh-47798: run_pipeline: small correctness cleanups
gpshead Apr 25, 2026
a348976
gh-47798: run_pipeline: kill all children before waiting on any
gpshead Apr 25, 2026
8b953b0
gh-47798: run_pipeline: small style cleanups
gpshead Apr 25, 2026
5175866
gh-47798: Document stderr=STDOUT behavior and add NEWS entry
gpshead Apr 25, 2026
bbdbd93
gh-47798: run_pipeline: cover gaps in test coverage
gpshead Apr 25, 2026
499ea81
gh-47798: Rename PipelineResult to CompletedPipeline
gpshead Apr 25, 2026
fd0231e
gh-47798: run_pipeline: small style and consistency nits
gpshead Apr 25, 2026
6e53bb9
gh-47798: run_pipeline: documentation gap fixes
gpshead Apr 25, 2026
56bbd4a
gh-47798: Restore close-on-EOF in Popen._communicate via factored helper
gpshead Apr 25, 2026
7ea824a
gh-47798: Test Popen.communicate resume after partial-write timeout
gpshead Apr 25, 2026
0091aa8
gh-47798: run_pipeline: cover shell, env, cwd, pass_fds forwarding
gpshead Apr 25, 2026
208c4ae
gh-47798: run_pipeline: cover stderr=PIPE success, errors=replace, br…
gpshead Apr 25, 2026
f3e0fea
gh-47798: Fix flake in test_pipeline_pass_fds
gpshead Apr 25, 2026
e65e55d
gh-47798: Cover Windows backend timeout in run_pipeline tests
gpshead Apr 25, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
211 changes: 211 additions & 0 deletions Doc/library/subprocess.rst
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,217 @@ underlying :class:`Popen` interface can be used directly.
*stdout* and *stderr* attributes added


.. function:: run_pipeline(*commands, stdin=None, input=None, \
stdout=None, stderr=None, capture_output=False, \
timeout=None, check=False, encoding=None, \
errors=None, text=None, env=None, \
**other_popen_kwargs)

Run a pipeline of commands connected via pipes, similar to shell pipelines.
Wait for all commands to complete, then return a :class:`CompletedPipeline`
instance.

Each positional argument should be a command (a list of strings, or a string
if ``shell=True``) to execute. The standard output of each command is
connected to the standard input of the next command in the pipeline.

This function requires at least two commands. For a single command, use
:func:`run` instead.

If *capture_output* is true, the standard output of the final command and
the standard error of all commands will be captured. All processes in the
pipeline share a single stderr pipe, so their error output will be
interleaved. The *stdout* and *stderr* arguments may not be supplied at
the same time as *capture_output*.

A *timeout* may be specified in seconds. If the timeout expires, all
child processes will be killed and waited for, and then a
:exc:`TimeoutExpired` exception will be raised.

The *input* argument is passed to the first command's stdin. If used, it
must be a byte sequence, or a string if *encoding* or *errors* is specified
or *text* is true.

If *check* is true, and any process in the pipeline exits with a non-zero
exit code, a :exc:`PipelineError` exception will be raised. This behavior
is similar to the shell's ``pipefail`` option.

If *encoding* or *errors* are specified, or *text* is true, file objects
are opened in text mode using the specified encoding and errors.

.. note::

When using ``text=True`` with ``capture_output=True`` or ``stderr=PIPE``,
be aware that stderr output from multiple processes may be interleaved
in ways that produce incomplete multi-byte character sequences. For
reliable text decoding of stderr, consider capturing in binary mode
and decoding manually with appropriate error handling, or use
``errors='replace'`` or ``errors='backslashreplace'``.

.. note::

Passing ``stderr=STDOUT`` redirects each child process's stderr to its
own stdout file descriptor. For non-final processes in the pipeline
this means stderr is merged into the next process's stdin, which is
rarely what callers want. Only the final process's stderr ends up at
the pipeline's stdout destination. To merge stderr-and-stdout output
from the whole pipeline, use ``capture_output=True`` (or pass an
explicit file descriptor as *stderr*) instead of ``STDOUT``.

.. note::

When stderr is captured (via ``capture_output=True`` or
``stderr=PIPE``), every command in the pipeline inherits a copy of the
same stderr write file descriptor. If any child spawns a daemon or
grandchild that keeps stderr open after the child exits, the parent's
read on the stderr pipe will not see EOF and :func:`run_pipeline` will
hang. This matches shell ``2>&1 | other`` behavior. If this is a
concern, either do not capture stderr, manage each command with an
individual :class:`Popen` so each has its own stderr pipe, or ensure
grandchildren fully detach (closing inherited fds) before
daemonizing.

If *stdin* is specified, it is connected to the first command's standard
input. If *stdout* is specified, it is connected to the last command's
standard output. When *stdout* is :data:`PIPE`, the output is available
in the returned :class:`CompletedPipeline`'s :attr:`~CompletedPipeline.stdout`
attribute. Other keyword arguments are passed to each :class:`Popen` call;
in particular *pass_fds* is forwarded to *every* command in the pipeline,
so any inheritable file descriptor passed via *pass_fds* is visible to all
children (unlike a shell, which can give each command its own fd set).
``close_fds=False`` is rejected because inherited copies of the
inter-process pipe ends in sibling children would prevent EOF from being
signaled and cause deadlocks.

Examples::

>>> import subprocess
>>> # Equivalent to: echo "hello world" | tr a-z A-Z
>>> result = subprocess.run_pipeline(
... ['echo', 'hello world'],
... ['tr', 'a-z', 'A-Z'],
... capture_output=True, text=True
... )
>>> result.stdout
'HELLO WORLD\n'
>>> result.returncodes
[0, 0]

>>> # Pipeline with three commands
>>> result = subprocess.run_pipeline(
... ['echo', 'one\ntwo\nthree'],
... ['sort'],
... ['head', '-n', '2'],
... capture_output=True, text=True
... )
>>> result.stdout
'one\nthree\n'

>>> # Using input parameter
>>> result = subprocess.run_pipeline(
... ['cat'],
... ['wc', '-l'],
... input='line1\nline2\nline3\n',
... capture_output=True, text=True
... )
>>> result.stdout.strip()
'3'

>>> # Error handling with check=True
>>> subprocess.run_pipeline(
... ['echo', 'hello'],
... ['false'], # exits with status 1
... check=True
... )
Traceback (most recent call last):
...
subprocess.PipelineError: Pipeline failed: command 1 ['false'] returned 1

.. versionadded:: next


.. class:: CompletedPipeline

The return value from :func:`run_pipeline`, representing a pipeline of
processes that have finished.

.. attribute:: commands

The list of commands used to launch the pipeline. Each command is a list
of strings (or a string if ``shell=True`` was used).

.. attribute:: returncodes

List of exit status codes for each command in the pipeline. Typically,
an exit status of 0 indicates that the command ran successfully.

A negative value ``-N`` indicates that the command was terminated by
signal ``N`` (POSIX only).

.. attribute:: returncode

Exit status of the final command in the pipeline. This is a convenience
property equivalent to ``returncodes[-1]``.

Note that this matches shell behavior *without* ``pipefail``: a zero
``returncode`` does not imply that earlier commands in the pipeline
succeeded. To check that all commands succeeded, use
:meth:`check_returncodes` or pass ``check=True`` to
:func:`run_pipeline`.

.. attribute:: stdout

Captured stdout from the final command in the pipeline. A bytes sequence,
or a string if :func:`run_pipeline` was called with an encoding, errors,
or ``text=True``. ``None`` if stdout was not captured.

.. attribute:: stderr

Captured stderr from all commands in the pipeline, combined. A bytes
sequence, or a string if :func:`run_pipeline` was called with an
encoding, errors, or ``text=True``. ``None`` if stderr was not captured.

.. method:: check_returncodes()

If any command's :attr:`returncode` is non-zero, raise a
:exc:`PipelineError`.

.. versionadded:: next


.. exception:: PipelineError

Subclass of :exc:`SubprocessError`, raised when a pipeline run by
:func:`run_pipeline` (with ``check=True``) contains one or more commands
that returned a non-zero exit status. This is similar to the shell's
``pipefail`` behavior.

.. attribute:: commands

List of commands that were used in the pipeline.
Comment thread
gpshead marked this conversation as resolved.

.. attribute:: returncodes

List of exit status codes for each command in the pipeline.

.. attribute:: stdout

Output of the final command if it was captured. Otherwise, ``None``.

.. attribute:: stderr

Combined stderr output of all commands if it was captured.
Otherwise, ``None``.

.. attribute:: failed

List of ``(index, command, returncode)`` tuples for each command
that returned a non-zero exit status. The *index* is the position
of the command in the pipeline (0-based).

.. versionadded:: next


.. _frequently-used-arguments:

Frequently Used Arguments
Expand Down
Loading
Loading