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