blob: 35faff6ba5b5a853f489857b93215f70d82bbf7c [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{popen2} ---
Fred Drake3aa70d61999-05-27 17:50:59 +00002 Subprocesses with accessible I/O streams}
Fred Drakeb91e9341998-07-23 17:59:49 +00003
Fred Drake93503ca1999-03-12 16:24:22 +00004\declaremodule{standard}{popen2}
Fred Drake8a9db992000-09-28 20:27:51 +00005 \platform{Unix, Windows}
Fred Drakea30e4691998-07-27 22:20:02 +00006\modulesynopsis{Subprocesses with accessible standard I/O streams.}
Fred Drakef6863c11999-03-02 16:37:17 +00007\sectionauthor{Drew Csillag}{drew_csillag@geocities.com}
Fred Drakeb91e9341998-07-23 17:59:49 +00008
Fred Drake6afad371998-04-28 14:28:15 +00009
Fred Drake8a9db992000-09-28 20:27:51 +000010This module allows you to spawn processes and connect to their
11input/output/error pipes and obtain their return codes under
12\UNIX{} and Windows.
Fred Drake6afad371998-04-28 14:28:15 +000013
Fred Drake8a9db992000-09-28 20:27:51 +000014Note that starting with Python 2.0, this functionality is available
15using functions from the \refmodule{os} module which have the same
16names as the factory functions here, but the order of the return
17values is more intuitive in the \refmodule{os} module variants.
Fred Drake6afad371998-04-28 14:28:15 +000018
Fred Drake8a9db992000-09-28 20:27:51 +000019The primary interface offered by this module is a trio of factory
20functions. For each of these, if \var{bufsize} is specified,
21it specifies the buffer size for the I/O pipes. \var{mode}, if
22provided, should be the string \code{'b'} or \code{'t'}; on Windows
23this is needed to determine whether the file objects should be opened
24in binary or text mode. The default value for \var{mode} is
25\code{'t'}.
26
Fred Drake098d7fa2001-09-11 19:56:51 +000027The only way to retrieve the return codes for the child processes is
28by 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()}
32functions, or the equivalent functions in the \refmodule{os} module.
Andrew M. Kuchling0188cdb2004-08-07 17:28:17 +000033(Note that the tuples returned by the \module{os} module's functions
34are in a different order from the ones returned by the \module{popen2} module.)
Fred Drake098d7fa2001-09-11 19:56:51 +000035
Fred Drake8a9db992000-09-28 20:27:51 +000036\begin{funcdesc}{popen2}{cmd\optional{, bufsize\optional{, mode}}}
37Executes \var{cmd} as a sub-process. Returns the file objects
38\code{(\var{child_stdout}, \var{child_stdin})}.
Fred Drake6afad371998-04-28 14:28:15 +000039\end{funcdesc}
40
Fred Drake8a9db992000-09-28 20:27:51 +000041\begin{funcdesc}{popen3}{cmd\optional{, bufsize\optional{, mode}}}
42Executes \var{cmd} as a sub-process. Returns the file objects
43\code{(\var{child_stdout}, \var{child_stdin}, \var{child_stderr})}.
Fred Drake6afad371998-04-28 14:28:15 +000044\end{funcdesc}
45
Fred Drake8a9db992000-09-28 20:27:51 +000046\begin{funcdesc}{popen4}{cmd\optional{, bufsize\optional{, mode}}}
47Executes \var{cmd} as a sub-process. Returns the file objects
48\code{(\var{child_stdout_and_stderr}, \var{child_stdin})}.
49\versionadded{2.0}
50\end{funcdesc}
51
52
53On \UNIX, a class defining the objects returned by the factory
54functions is also available. These are not used for the Windows
55implementation, and are not available on that platform.
Fred Drake6afad371998-04-28 14:28:15 +000056
57\begin{classdesc}{Popen3}{cmd\optional{, capturestderr\optional{, bufsize}}}
58This class represents a child process. Normally, \class{Popen3}
Fred Drake8a9db992000-09-28 20:27:51 +000059instances are created using the \function{popen2()} and
60\function{popen3()} factory functions described above.
Fred Drake6afad371998-04-28 14:28:15 +000061
Andrew M. Kuchling93cf58b2003-02-06 18:04:43 +000062If not using one of the helper functions to create \class{Popen3}
Fred Drake6afad371998-04-28 14:28:15 +000063objects, the parameter \var{cmd} is the shell command to execute in a
64sub-process. The \var{capturestderr} flag, if true, specifies that
65the object should capture standard error output of the child process.
66The default is false. If the \var{bufsize} parameter is specified, it
67specifies the size of the I/O buffers to/from the child process.
68\end{classdesc}
69
Fred Drake8a9db992000-09-28 20:27:51 +000070\begin{classdesc}{Popen4}{cmd\optional{, bufsize}}
71Similar to \class{Popen3}, but always captures standard error into the
72same file object as standard output. These are typically created
73using \function{popen4()}.
74\versionadded{2.0}
75\end{classdesc}
Fred Drake6afad371998-04-28 14:28:15 +000076
Fred Drake6afad371998-04-28 14:28:15 +000077
Fred Drake8a9db992000-09-28 20:27:51 +000078\subsection{Popen3 and Popen4 Objects \label{popen3-objects}}
79
80Instances of the \class{Popen3} and \class{Popen4} classes have the
81following methods:
Fred Drake6afad371998-04-28 14:28:15 +000082
83\begin{methoddesc}{poll}{}
84Returns \code{-1} if child process hasn't completed yet, or its return
85code otherwise.
86\end{methoddesc}
87
88\begin{methoddesc}{wait}{}
Fred Drake45c23e62001-07-06 17:17:12 +000089Waits for and returns the status code of the child process. The
90status code encodes both the return code of the process and
91information about whether it exited using the \cfunction{exit()}
92system call or died due to a signal. Functions to help interpret the
93status code are defined in the \refmodule{os} module; see section
94\ref{os-process} for the \function{W\var{*}()} family of functions.
Fred Drake6afad371998-04-28 14:28:15 +000095\end{methoddesc}
96
97
Fred Drake8a9db992000-09-28 20:27:51 +000098The following attributes are also available:
Fred Drake6afad371998-04-28 14:28:15 +000099
Fred Drake3aa70d61999-05-27 17:50:59 +0000100\begin{memberdesc}{fromchild}
Fred Drake8a9db992000-09-28 20:27:51 +0000101A file object that provides output from the child process. For
102\class{Popen4} instances, this will provide both the standard output
103and standard error streams.
Fred Drake3aa70d61999-05-27 17:50:59 +0000104\end{memberdesc}
Fred Drake6afad371998-04-28 14:28:15 +0000105
Fred Drake3aa70d61999-05-27 17:50:59 +0000106\begin{memberdesc}{tochild}
Fred Drake6afad371998-04-28 14:28:15 +0000107A file object that provides input to the child process.
Fred Drake3aa70d61999-05-27 17:50:59 +0000108\end{memberdesc}
Fred Drake6afad371998-04-28 14:28:15 +0000109
Fred Drake3aa70d61999-05-27 17:50:59 +0000110\begin{memberdesc}{childerr}
Andrew M. Kuchling91ca8de2003-12-23 17:01:38 +0000111A file object that provides error output from the child process, if
112\var{capturestderr} was true for the constructor, otherwise
113\code{None}. This will always be \code{None} for \class{Popen4}
114instances.
Fred Drake3aa70d61999-05-27 17:50:59 +0000115\end{memberdesc}
116
117\begin{memberdesc}{pid}
118The process ID of the child process.
119\end{memberdesc}
Fred Drake9ea01d42002-06-18 20:30:37 +0000120
121
122\subsection{Flow Control Issues \label{popen2-flow-control}}
123
124Any time you are working with any form of inter-process communication,
125control flow needs to be carefully thought out. This remains the case
126with the file objects provided by this module (or the \refmodule{os}
127module equivalents).
128
129% Example explanation and suggested work-arounds substantially stolen
130% from Martin von Löwis:
131% http://mail.python.org/pipermail/python-dev/2000-September/009460.html
132
133When reading output from a child process that writes a lot of data to
134standard error while the parent is reading from the child's standard
Andrew M. Kuchling93cf58b2003-02-06 18:04:43 +0000135output, a deadlock can occur. A similar situation can occur with other
Fred Drake9ea01d42002-06-18 20:30:37 +0000136combinations of reads and writes. The essential factors are that more
Fred Drakef4bf7aa2002-06-18 20:38:05 +0000137than \constant{_PC_PIPE_BUF} bytes are being written by one process in
Fred Drake9ea01d42002-06-18 20:30:37 +0000138a blocking fashion, while the other process is reading from the other
139process, also in a blocking fashion.
140
141There are several ways to deal with this situation.
142
143The simplest application change, in many cases, will be to follow this
144model in the parent process:
145
146\begin{verbatim}
147import popen2
148
149r, w, e = popen2.popen3('python slave.py')
150e.readlines()
151r.readlines()
152r.close()
153e.close()
154w.close()
155\end{verbatim}
156
157with code like this in the child:
158
159\begin{verbatim}
160import os
161import sys
162
163# note that each of these print statements
164# writes a single long string
165
166print >>sys.stderr, 400 * 'this is a test\n'
167os.close(sys.stderr.fileno())
168print >>sys.stdout, 400 * 'this is another test\n'
169\end{verbatim}
170
171In particular, note that \code{sys.stderr} must be closed after
172writing all data, or \method{readlines()} won't return. Also note
173that \function{os.close()} must be used, as \code{sys.stderr.close()}
174won't close \code{stderr} (otherwise assigning to \code{sys.stderr}
175will silently close it, so no further errors can be printed).
176
177Applications which need to support a more general approach should
178integrate I/O over pipes with their \function{select()} loops, or use
179separate threads to read each of the individual files provided by
180whichever \function{popen*()} function or \class{Popen*} class was
181used.