subprocess.Popen vs subprocess.run in Python
Codemia Knowledge Hub ·
subprocess.run() is the high-level API: it starts a command, waits for it to finish and returns the result. subprocess.Popen() is the lower-level API that run() is built on: it starts the process and returns immediately, leaving you to decide when to wait and how to handle its pipes. Use run() for one-shot commands and Popen() for streaming I/O, background execution, pipelines or manual lifecycle control.
subprocess.run() blocks and returns a CompletedProcess
This is the modern default for straightforward command execution. It is readable, and safe when you pass an argument list.
To raise an exception automatically when the command fails:
That raises CalledProcessError if the exit code is non-zero.
subprocess.Popen() returns a process object immediately
It is more verbose, but you decide when to wait, how to read output, whether to poll, and how to combine several child processes. Using it where run() would do just adds complexity.
Choosing between them
Use run() when all of these are true:
- you want to wait for completion immediately
- you do not need interactive communication while the process runs
- you want the simplest API
Use Popen() when you need at least one of these:
- process starts in the background
- stdout or stderr must be handled incrementally
- the child process needs interactive input
- you are building a pipeline or supervising several processes
run() wraps the common "start, wait, capture, return" workflow around the same machinery, which is why many older Popen() examples read more clearly when rewritten with run().
Streaming output line by line with Popen()
run() cannot show output while the process is still running. Popen() can:
This is useful for long-running jobs, build tools and anything where you want live progress instead of the final output buffer.
Polling a background process
Popen() also fits when the parent has other work to do while the child runs:
That would be awkward with run(), because run() blocks until completion.
Pipe deadlocks and shell=True
With Popen(), always consume or redirect the pipes you open. If the child writes enough data to a pipe that nobody reads, it blocks.
Be careful with shell=True in either API. If parts of the command come from user input, passing an argument list is safer than a single shell string.
Related reading
- What is the difference between tf.keras and tf.python.keras?
- What is the difference between the AWS boto and boto3
- What is the difference between types.coroutine and asyncio.coroutine decorators?
- what is the difference between vagrant, docker, virtualenv or just a virtual machine?
- What is the difference between venv, pyvenv, pyenv, virtualenv, virtualenvwrapper, pipenv, etc?
- What is the difference between X_test, X_train, y_test, y_train in sklearn?
- What is the difference in installing tensorflow with pip command and conda or directing cloning?
- What is the difference in purpose between tf.py_function and tf.function?
System design interview problems on this topic
- Design a Web Crawler: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a URL Shortener (TinyURL): solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.