Use subprocess.run() when Python can wait for a command to finish. Choose subprocess.Popen() when you need a handle to poll or stop a running child. For bounded captured output, use communicate() to drain both pipes. If you read one stream and wait for the child, it can stall when the other stream fills its buffer.
When do you need a live process handle?
Describe the interaction before choosing the call. A program that launches a known tool, obtains a result, and decides what to do next can use run(). Popen is useful when you need to poll a process while doing something else or manage a longer interaction.
Owning a process handle includes decisions about closing files, interruption, and exit. You should know who stops the child if the parent encounters an exception. If you do not need to make those decisions, the simpler interface is easier to read and review.
Walk through a bounded example
This example uses a known local child. The parent stops it if communication times out and then calls communicate() again to finish the cleanup.
import subprocess
import sys
with subprocess.Popen(
[sys.executable, "-c", "print('finished')"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
) as process:
try:
stdout, stderr = process.communicate(timeout=5)
except subprocess.TimeoutExpired:
process.kill()
process.communicate()
raise
if process.returncode != 0:
raise RuntimeError(f"Child exited with status {process.returncode}")
print(stdout.rstrip())
Expect finished. The child in this example exits almost immediately. The timeout branch shows what you need to do for a slower child. Stop it, then finish reading its pipes so the parent can collect its exit status.
Know what the timeout does
A communicate() timeout does not terminate a Popen child on its own. The cleanup above is explicit. Whether a real tool should receive a gentler shutdown request depends on its contract and platform. Stopping a child does not necessarily stop descendants it created.
If an operation times out after emitting some output, keep the result marked incomplete. A program can report that it opened a file before it has written a usable report. Partial output may help diagnose the failure, but the application needs a separate way to know that the job finished.
Do useful work between polls
An operator tool might start a local export, display a busy state, and remain responsive while the child works. A process handle can report whether the child has exited. Polling alone does not drain pipes. If you only need a final result, consider running the simpler synchronous call in the part of the application already responsible for background work.
Keep reading and cancellation responsibilities clear. Two readers consuming the same stream can observe different chunks of output. If the child exits while the application processes a cancellation, decide how to represent the final result. A reliable operator message distinguishes whether the process was started, exited, or interrupted.
Make a repeatable interruption test
Start a known local test child that sleeps, then emits a marker. Apply a shorter timeout and verify the parent reports failure and reaps that child. Run the same example with enough time to verify the success output. A program that spawns descendants needs a platform-specific teardown design beyond this one-child example.
If a previous report still exists after an interruption, mark it as the earlier verified result. Do not assume the most recently touched output is complete unless your write protocol makes that guarantee.
Account for buffering and volume
Line-by-line visibility depends on whether the child flushes output and whether the parent keeps reading every captured stream. A quiet interval can represent an idle child, buffering, or a genuine problem. Blocking on one read does not distinguish them.
Use communicate() for short predictable results. For longer or continuous output, choose a streaming design with a clear protocol or redirect to files with size and rotation rules. Test a slow child, a nonzero exit, and a workload with enough output to exercise the interface before depending on continuous progress messages.