PyDocs

Nav

Concurrent Execution

Popen

subprocess

class · Added in 3.9 (source) POSIX
  • 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=(), *, group=None, extra_groups=None, user=None, umask=-1, encoding=None, errors=None, text=None, pipesize=-1, process_group=None)
Execute a child program in a new process.

For a complete description of the arguments see the Python documentation.

Arguments:
  args: A string, or a sequence of program arguments.

  bufsize: supplied as the buffering argument to the open() function when
      creating the stdin/stdout/stderr pipe file objects

  executable: A replacement program to execute.

  stdin, stdout and stderr: These specify the executed programs' standard
      input, standard output and standard error file handles, respectively.

  preexec_fn: (POSIX only) An object to be called in the child process
      just before the child is executed.

  close_fds: Controls closing or inheriting of file descriptors.

  shell: If true, the command will be executed through the shell.

  cwd: Sets the current directory before the child is executed.

  env: Defines the environment variables for the new process.

  text: If true, decode stdin, stdout and stderr using the given encoding
      (if set) or the system default otherwise.

  universal_newlines: Alias of text, provided for backwards compatibility.

  startupinfo and creationflags (Windows only)

  restore_signals (POSIX only)

  start_new_session (POSIX only)

  process_group (POSIX only)

  group (POSIX only)

  extra_groups (POSIX only)

  user (POSIX only)

  umask (POSIX only)

  pass_fds (POSIX only)

  encoding and errors: Text mode encoding and error handling to use for
      file objects stdin, stdout and stderr.

Attributes:
    stdin, stdout, stderr, pid, returncode
Changed in 3.2, 3.3.1, 3.6, 3.7, 3.8, 3.10, 3.11, 3.12
  • 3.2: The default for close_fds was changed from False to what is described above.
  • 3.2: The pass_fds parameter was added.
  • 3.2: restore_signals was added.
  • 3.2: start_new_session was added.
  • 3.2: Added context manager support.
  • 3.3.1: bufsize now defaults to -1 to enable buffering by default to match the behavior that most code expects. In versions prior to Python 3.2.4 and 3.3.1 it incorrectly defaulted to 0 which was unbuffered and allowed short reads. This was unintentional and did not match the behavior of Python 2 as most code expected.
  • 3.6: args parameter accepts a path-like object if shell is False and a sequence containing path-like objects on POSIX.
  • 3.6: executable parameter accepts a path-like object on POSIX.
  • 3.6: cwd parameter accepts a path-like object on POSIX.
  • 3.6: Popen destructor now emits a ResourceWarning warning if the child process is still running.
  • 3.6: encoding and errors were added.
  • 3.7: On Windows the default for close_fds was changed from False to True when redirecting the standard handles. It’s now possible to set close_fds to True when redirecting the standard handles.
  • 3.7: cwd parameter accepts a path-like object on Windows.
  • 3.7: text was added as a more readable alias for universal_newlines.
  • 3.8: args parameter accepts a path-like object if shell is False and a sequence containing bytes and path-like objects on Windows.
  • 3.8: executable parameter accepts a bytes and path-like object on Windows.
  • 3.8: The preexec_fn parameter is no longer supported in subinterpreters. The use of the parameter in a subinterpreter raises RuntimeError. The new restriction may affect applications that are deployed in mod_wsgi, uWSGI, and other embedded environments.
  • 3.8: cwd parameter accepts a bytes object on Windows.
  • 3.8: Popen can use os.posix_spawn() in some cases for better performance. On Windows Subsystem for Linux and QEMU User Emulation, Popen constructor using os.posix_spawn() no longer raise an exception on errors like missing program, but the child process fails with a non-zero returncode.
  • 3.10: Added the pipesize parameter.
  • 3.11: process_group was added.
  • 3.12: Changed Windows shell search order for shell=True. The current directory and %PATH% are replaced with %COMSPEC% and %SystemRoot%\System32\cmd.exe. As a result, dropping a malicious program named cmd.exe into a current directory no longer works.
Members of Popen

a

args
The args argument as it was passed to Popen – a sequence of program arguments or else a single string.

c

communicate
Interact with process: Send data to stdin and close it.

k

kill
Kill the process with SIGKILL

p

pid
The process ID of the child process.
poll
Check if child process has terminated.

r

returncode
The child return code.

s

send_signal
Send a signal to the process.
stderr
If the stderr argument was PIPE, this attribute is a readable stream object as returned by open().
stdin
If the stdin argument was PIPE, this attribute is a writeable stream object as returned by open().
stdout
If the stdout argument was PIPE, this attribute is a readable stream object as returned by open().

t

terminate
Terminate the process with SIGTERM

w

wait
Wait for child process to terminate; returns self.returncode.
1 Example
>>> import shlex, subprocess
>>> command_line = input()
/bin/vikings -input eggs.txt -output "spam spam.txt" -cmd "echo '$MONEY'"
>>> args = shlex.split(command_line)
>>> print(args)
['/bin/vikings', '-input', 'eggs.txt', '-output', 'spam spam.txt', '-cmd', "echo '$MONEY'"]
>>> p = subprocess.Popen(args) # Success!
from the Python docs
See Also
No see-alsos for subprocess/Popen
0 Notes
No notes for Popen