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