Why does a Python subprocess hang only when stdout is redirected to PIPE?
07:49 14 Aug 2026

I'm running an external command using Python's subprocess module.

This works:

import subprocess

p = subprocess.Popen(["my_command"])
p.wait()

But when I capture its output:

p = subprocess.Popen(
    ["my_command"],
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE
)

p.wait()

the process occasionally hangs indefinitely, especially when the command produces a lot of output.

If I remove stdout=subprocess.PIPE and stderr=subprocess.PIPE, it completes normally.

Why can redirecting stdout/stderr cause the child process to hang? Is the pipe buffer becoming full while the parent is waiting?

What is the correct way to capture both stdout and stderr without risking this deadlock?

python subprocess pipe