blob: 322a9b96352a42bbce5c212cbb5c0fafc38de957 [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 Drakea30e4691998-07-27 22:20:02 +00005\modulesynopsis{Subprocesses with accessible standard I/O streams.}
Fred Drakef6863c11999-03-02 16:37:17 +00006\sectionauthor{Drew Csillag}{drew_csillag@geocities.com}
Fred Drakeb91e9341998-07-23 17:59:49 +00007
Fred Drake6afad371998-04-28 14:28:15 +00008
Fred Drake8a9db992000-09-28 20:27:51 +00009This module allows you to spawn processes and connect to their
10input/output/error pipes and obtain their return codes under
11\UNIX{} and Windows.
Fred Drake6afad371998-04-28 14:28:15 +000012
Andrew M. Kuchlingd3aad012006-10-27 14:53:41 +000013The \module{subprocess} module provides more powerful facilities for
14spawning new processes and retrieving their results. Using the
15\module{subprocess} module is preferable to using the \module{popen2}
16module.
Fred Drake6afad371998-04-28 14:28:15 +000017
Fred Drake8a9db992000-09-28 20:27:51 +000018The primary interface offered by this module is a trio of factory
19functions. For each of these, if \var{bufsize} is specified,
20it specifies the buffer size for the I/O pipes. \var{mode}, if
21provided, should be the string \code{'b'} or \code{'t'}; on Windows
22this is needed to determine whether the file objects should be opened
23in binary or text mode. The default value for \var{mode} is
24\code{'t'}.
25
Johannes Gijsbers9fc97892004-10-11 18:12:20 +000026On \UNIX, \var{cmd} may be a sequence, in which case arguments will be passed
27directly to the program without shell intervention (as with
28\function{os.spawnv()}). If \var{cmd} is a string it will be passed to the
29shell (as with \function{os.system()}).
30
Fred Drake098d7fa2001-09-11 19:56:51 +000031The only way to retrieve the return codes for the child processes is
32by 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()}
36functions, or the equivalent functions in the \refmodule{os} module.
Fred Drake151df072004-08-09 14:12:05 +000037(Note that the tuples returned by the \refmodule{os} module's functions
38are in a different order from the ones returned by the \module{popen2}
39module.)
Fred Drake098d7fa2001-09-11 19:56:51 +000040
Fred Drake8a9db992000-09-28 20:27:51 +000041\begin{funcdesc}{popen2}{cmd\optional{, bufsize\optional{, mode}}}
42Executes \var{cmd} as a sub-process. Returns the file objects
43\code{(\var{child_stdout}, \var{child_stdin})}.
Fred Drake6afad371998-04-28 14:28:15 +000044\end{funcdesc}
45
Fred Drake8a9db992000-09-28 20:27:51 +000046\begin{funcdesc}{popen3}{cmd\optional{, bufsize\optional{, mode}}}
47Executes \var{cmd} as a sub-process. Returns the file objects
48\code{(\var{child_stdout}, \var{child_stdin}, \var{child_stderr})}.
Fred Drake6afad371998-04-28 14:28:15 +000049\end{funcdesc}
50
Fred Drake8a9db992000-09-28 20:27:51 +000051\begin{funcdesc}{popen4}{cmd\optional{, bufsize\optional{, mode}}}
52Executes \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
58On \UNIX, a class defining the objects returned by the factory
59functions is also available. These are not used for the Windows
60implementation, and are not available on that platform.
Fred Drake6afad371998-04-28 14:28:15 +000061
62\begin{classdesc}{Popen3}{cmd\optional{, capturestderr\optional{, bufsize}}}
63This class represents a child process. Normally, \class{Popen3}
Fred Drake8a9db992000-09-28 20:27:51 +000064instances are created using the \function{popen2()} and
65\function{popen3()} factory functions described above.
Fred Drake6afad371998-04-28 14:28:15 +000066
Andrew M. Kuchling93cf58b2003-02-06 18:04:43 +000067If not using one of the helper functions to create \class{Popen3}
Fred Drake6afad371998-04-28 14:28:15 +000068objects, the parameter \var{cmd} is the shell command to execute in a
69sub-process. The \var{capturestderr} flag, if true, specifies that
70the object should capture standard error output of the child process.
71The default is false. If the \var{bufsize} parameter is specified, it
72specifies the size of the I/O buffers to/from the child process.
73\end{classdesc}
74
Fred Drake8a9db992000-09-28 20:27:51 +000075\begin{classdesc}{Popen4}{cmd\optional{, bufsize}}
76Similar to \class{Popen3}, but always captures standard error into the
77same file object as standard output. These are typically created
78using \function{popen4()}.
79\versionadded{2.0}
80\end{classdesc}
Fred Drake6afad371998-04-28 14:28:15 +000081
Fred Drake8a9db992000-09-28 20:27:51 +000082\subsection{Popen3 and Popen4 Objects \label{popen3-objects}}
83
84Instances of the \class{Popen3} and \class{Popen4} classes have the
85following methods:
Fred Drake6afad371998-04-28 14:28:15 +000086
Georg Brandlae91afd2007-04-01 22:39:10 +000087\begin{methoddesc}[Popen3]{poll}{}
Fred Drake6afad371998-04-28 14:28:15 +000088Returns \code{-1} if child process hasn't completed yet, or its return
89code otherwise.
90\end{methoddesc}
91
Georg Brandlae91afd2007-04-01 22:39:10 +000092\begin{methoddesc}[Popen3]{wait}{}
Fred Drake45c23e62001-07-06 17:17:12 +000093Waits for and returns the status code of the child process. The
94status code encodes both the return code of the process and
95information about whether it exited using the \cfunction{exit()}
96system call or died due to a signal. Functions to help interpret the
97status code are defined in the \refmodule{os} module; see section
98\ref{os-process} for the \function{W\var{*}()} family of functions.
Fred Drake6afad371998-04-28 14:28:15 +000099\end{methoddesc}
100
101
Fred Drake8a9db992000-09-28 20:27:51 +0000102The following attributes are also available:
Fred Drake6afad371998-04-28 14:28:15 +0000103
Georg Brandlae91afd2007-04-01 22:39:10 +0000104\begin{memberdesc}[Popen3]{fromchild}
Fred Drake8a9db992000-09-28 20:27:51 +0000105A file object that provides output from the child process. For
106\class{Popen4} instances, this will provide both the standard output
107and standard error streams.
Fred Drake3aa70d61999-05-27 17:50:59 +0000108\end{memberdesc}
Fred Drake6afad371998-04-28 14:28:15 +0000109
Georg Brandlae91afd2007-04-01 22:39:10 +0000110\begin{memberdesc}[Popen3]{tochild}
Fred Drake6afad371998-04-28 14:28:15 +0000111A file object that provides input to the child process.
Fred Drake3aa70d61999-05-27 17:50:59 +0000112\end{memberdesc}
Fred Drake6afad371998-04-28 14:28:15 +0000113
Georg Brandlae91afd2007-04-01 22:39:10 +0000114\begin{memberdesc}[Popen3]{childerr}
Andrew M. Kuchling91ca8de2003-12-23 17:01:38 +0000115A 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}
118instances.
Fred Drake3aa70d61999-05-27 17:50:59 +0000119\end{memberdesc}
120
Georg Brandlae91afd2007-04-01 22:39:10 +0000121\begin{memberdesc}[Popen3]{pid}
Fred Drake3aa70d61999-05-27 17:50:59 +0000122The process ID of the child process.
123\end{memberdesc}
Fred Drake9ea01d42002-06-18 20:30:37 +0000124
125
126\subsection{Flow Control Issues \label{popen2-flow-control}}
127
128Any time you are working with any form of inter-process communication,
129control flow needs to be carefully thought out. This remains the case
130with the file objects provided by this module (or the \refmodule{os}
131module 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
137When reading output from a child process that writes a lot of data to
138standard error while the parent is reading from the child's standard
Andrew M. Kuchling93cf58b2003-02-06 18:04:43 +0000139output, a deadlock can occur. A similar situation can occur with other
Fred Drake9ea01d42002-06-18 20:30:37 +0000140combinations of reads and writes. The essential factors are that more
Fred Drakef4bf7aa2002-06-18 20:38:05 +0000141than \constant{_PC_PIPE_BUF} bytes are being written by one process in
Fred Drake9ea01d42002-06-18 20:30:37 +0000142a blocking fashion, while the other process is reading from the other
143process, also in a blocking fashion.
144
145There are several ways to deal with this situation.
146
147The simplest application change, in many cases, will be to follow this
148model in the parent process:
149
150\begin{verbatim}
151import popen2
152
153r, w, e = popen2.popen3('python slave.py')
154e.readlines()
155r.readlines()
156r.close()
157e.close()
158w.close()
159\end{verbatim}
160
161with code like this in the child:
162
163\begin{verbatim}
164import os
165import sys
166
167# note that each of these print statements
168# writes a single long string
169
170print >>sys.stderr, 400 * 'this is a test\n'
171os.close(sys.stderr.fileno())
172print >>sys.stdout, 400 * 'this is another test\n'
173\end{verbatim}
174
175In particular, note that \code{sys.stderr} must be closed after
176writing all data, or \method{readlines()} won't return. Also note
177that \function{os.close()} must be used, as \code{sys.stderr.close()}
178won't close \code{stderr} (otherwise assigning to \code{sys.stderr}
179will silently close it, so no further errors can be printed).
180
181Applications which need to support a more general approach should
182integrate I/O over pipes with their \function{select()} loops, or use
183separate threads to read each of the individual files provided by
184whichever \function{popen*()} function or \class{Popen*} class was
185used.
Andrew M. Kuchlingd3aad012006-10-27 14:53:41 +0000186
187\begin{seealso}
188 \seemodule{subprocess}{Module for spawning and managing subprocesses.}
189\end{seealso}