2 :mod:`popen2` --- Subprocesses with accessible I/O streams
3 ==========================================================
6 :synopsis: Subprocesses with accessible standard I/O streams.
8 .. sectionauthor:: Drew Csillag <drew_csillag@geocities.com>
12 This module is obsolete. Use the :mod:`subprocess` module.
14 This module allows you to spawn processes and connect to their
15 input/output/error pipes and obtain their return codes under Unix and Windows.
17 The :mod:`subprocess` module provides more powerful facilities for spawning new
18 processes and retrieving their results. Using the :mod:`subprocess` module is
19 preferable to using the :mod:`popen2` module.
21 The primary interface offered by this module is a trio of factory functions.
22 For each of these, if *bufsize* is specified, it specifies the buffer size for
23 the I/O pipes. *mode*, if provided, should be the string ``'b'`` or ``'t'``; on
24 Windows this is needed to determine whether the file objects should be opened in
25 binary or text mode. The default value for *mode* is ``'t'``.
27 On Unix, *cmd* may be a sequence, in which case arguments will be passed
28 directly to the program without shell intervention (as with :func:`os.spawnv`).
29 If *cmd* is a string it will be passed to the shell (as with :func:`os.system`).
31 The only way to retrieve the return codes for the child processes is by using
32 the :meth:`poll` or :meth:`wait` methods on the :class:`Popen3` and
33 :class:`Popen4` classes; these are only available on Unix. This information is
34 not available when using the :func:`popen2`, :func:`popen3`, and :func:`popen4`
35 functions, or the equivalent functions in the :mod:`os` module. (Note that the
36 tuples returned by the :mod:`os` module's functions are in a different order
37 from the ones returned by the :mod:`popen2` module.)
40 .. function:: popen2(cmd[, bufsize[, mode]])
42 Executes *cmd* as a sub-process. Returns the file objects ``(child_stdout,
46 .. function:: popen3(cmd[, bufsize[, mode]])
48 Executes *cmd* as a sub-process. Returns the file objects ``(child_stdout,
49 child_stdin, child_stderr)``.
52 .. function:: popen4(cmd[, bufsize[, mode]])
54 Executes *cmd* as a sub-process. Returns the file objects
55 ``(child_stdout_and_stderr, child_stdin)``.
59 On Unix, a class defining the objects returned by the factory functions is also
60 available. These are not used for the Windows implementation, and are not
61 available on that platform.
64 .. class:: Popen3(cmd[, capturestderr[, bufsize]])
66 This class represents a child process. Normally, :class:`Popen3` instances are
67 created using the :func:`popen2` and :func:`popen3` factory functions described
70 If not using one of the helper functions to create :class:`Popen3` objects, the
71 parameter *cmd* is the shell command to execute in a sub-process. The
72 *capturestderr* flag, if true, specifies that the object should capture standard
73 error output of the child process. The default is false. If the *bufsize*
74 parameter is specified, it specifies the size of the I/O buffers to/from the
78 .. class:: Popen4(cmd[, bufsize])
80 Similar to :class:`Popen3`, but always captures standard error into the same
81 file object as standard output. These are typically created using
89 Popen3 and Popen4 Objects
90 -------------------------
92 Instances of the :class:`Popen3` and :class:`Popen4` classes have the following
96 .. method:: Popen3.poll()
98 Returns ``-1`` if child process hasn't completed yet, or its status code
99 (see :meth:`wait`) otherwise.
102 .. method:: Popen3.wait()
104 Waits for and returns the status code of the child process. The status code
105 encodes both the return code of the process and information about whether it
106 exited using the :cfunc:`exit` system call or died due to a signal. Functions
107 to help interpret the status code are defined in the :mod:`os` module; see
108 section :ref:`os-process` for the :func:`W\*` family of functions.
110 The following attributes are also available:
113 .. attribute:: Popen3.fromchild
115 A file object that provides output from the child process. For :class:`Popen4`
116 instances, this will provide both the standard output and standard error
120 .. attribute:: Popen3.tochild
122 A file object that provides input to the child process.
125 .. attribute:: Popen3.childerr
127 A file object that provides error output from the child process, if
128 *capturestderr* was true for the constructor, otherwise ``None``. This will
129 always be ``None`` for :class:`Popen4` instances.
132 .. attribute:: Popen3.pid
134 The process ID of the child process.
137 .. _popen2-flow-control:
142 Any time you are working with any form of inter-process communication, control
143 flow needs to be carefully thought out. This remains the case with the file
144 objects provided by this module (or the :mod:`os` module equivalents).
146 When reading output from a child process that writes a lot of data to standard
147 error while the parent is reading from the child's standard output, a deadlock
148 can occur. A similar situation can occur with other combinations of reads and
149 writes. The essential factors are that more than :const:`_PC_PIPE_BUF` bytes
150 are being written by one process in a blocking fashion, while the other process
151 is reading from the other process, also in a blocking fashion.
153 .. Example explanation and suggested work-arounds substantially stolen
154 from Martin von Löwis:
155 http://mail.python.org/pipermail/python-dev/2000-September/009460.html
157 There are several ways to deal with this situation.
159 The simplest application change, in many cases, will be to follow this model in
164 r, w, e = popen2.popen3('python slave.py')
171 with code like this in the child::
176 # note that each of these print statements
177 # writes a single long string
179 print >>sys.stderr, 400 * 'this is a test\n'
180 os.close(sys.stderr.fileno())
181 print >>sys.stdout, 400 * 'this is another test\n'
183 In particular, note that ``sys.stderr`` must be closed after writing all data,
184 or :meth:`readlines` won't return. Also note that :func:`os.close` must be
185 used, as ``sys.stderr.close()`` won't close ``stderr`` (otherwise assigning to
186 ``sys.stderr`` will silently close it, so no further errors can be printed).
188 Applications which need to support a more general approach should integrate I/O
189 over pipes with their :func:`select` loops, or use separate threads to read each
190 of the individual files provided by whichever :func:`popen\*` function or
191 :class:`Popen\*` class was used.
196 Module :mod:`subprocess`
197 Module for spawning and managing subprocesses.