On this page
subprocess.Popen
Prev: subprocess.getstatusoutput | Up: subprocess | Next: subprocess.run
Rel: subprocess.call, subprocess.CalledProcessError, subprocess.check_call, subprocess.check_output
Ref: docs.python.org — subprocess.Popen
Tags: #public #python3 #stdlib #subprocess
Import
import subprocess
from subprocess import Popen
Signature & Overview
subprocess.Popen(args, bufsize=-1, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=True, shell=False, cwd=None, env=None, universal_newlines=None, startupinfo=None, creationflags=0, restore_signals=True, start_new_session=False, pass_fds=(), *, user=None, group=None, extra_groups=None, encoding=None, errors=None, text=None, umask=-1, pipesize=-1, process_group=None)
Execute a child program in a new process.
Methods & Attributes
Popen.communicate(self, input=None, timeout=None)— Interact with process: Send data to stdin and close it.Popen.kill(self)— Kill the process with SIGKILLPopen.poll(self)— Check if child process has terminated. Set and return returncode attribute.Popen.send_signal(self, sig)— Send a signal to the process.Popen.terminate(self)— Terminate the process with SIGTERMPopen.universal_newlines(...)—Popen.universal_newlinesmethod/property.Popen.wait(self, timeout=None)— Wait for child process to terminate; returns self.returncode.
Usage
import subprocess
import sys
with subprocess.Popen(
[sys.executable, "-c", "import sys; print(sys.stdin.read().upper())"],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
text=True,
) as proc:
out, _ = proc.communicate("hello")
assert out.strip() == "HELLO" and proc.returncode == 0
Gotchas
- Prefer
subprocess.run()unless streaming or running concurrently:Popenis the low-level non-blocking constructor. - Avoid pipe deadlocks: Use
proc.communicate()instead of manually callingproc.stdin.write()+proc.stdout.read()on large outputs. - Always use
with subprocess.Popen(...) as proc:: Ensures pipes are closed and child processes are reaped.
REPL Inspect
from subprocess import Popen
help(Popen)
Linked from
Related notes
- tuts/python3/stdlib_module/subprocess/getstatusoutput Linked here; links to this page; shared tags: #stdlib, #subprocess
- tuts/python3/stdlib_module/subprocess/run Linked here; links to this page; shared tags: #stdlib, #subprocess
- tuts/python3/stdlib_module/subprocess Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/subprocess/call Linked here; shared tags: #stdlib, #subprocess
- tuts/python3/stdlib_module/subprocess/CalledProcessError Linked here; shared tags: #stdlib, #subprocess
- tuts/python3/stdlib_module/subprocess/check_call Linked here; shared tags: #stdlib, #subprocess
- tuts/python3/stdlib_module/subprocess/check_output Linked here; shared tags: #stdlib, #subprocess
- tuts/python3/stdlib_module/subprocess/CompletedProcess shared tags: #stdlib, #subprocess
- tuts/python3/stdlib_module/subprocess/getoutput shared tags: #stdlib, #subprocess
- tuts/python3/stdlib_module/subprocess/SubprocessError shared tags: #stdlib, #subprocess