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