blob: add0af130cf8761f62c3e749eba6e42cb8f43997 [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
27\begin{funcdesc}{popen2}{cmd\optional{, bufsize\optional{, mode}}}
28Executes \var{cmd} as a sub-process. Returns the file objects
29\code{(\var{child_stdout}, \var{child_stdin})}.
Fred Drake6afad371998-04-28 14:28:15 +000030\end{funcdesc}
31
Fred Drake8a9db992000-09-28 20:27:51 +000032\begin{funcdesc}{popen3}{cmd\optional{, bufsize\optional{, mode}}}
33Executes \var{cmd} as a sub-process. Returns the file objects
34\code{(\var{child_stdout}, \var{child_stdin}, \var{child_stderr})}.
Fred Drake6afad371998-04-28 14:28:15 +000035\end{funcdesc}
36
Fred Drake8a9db992000-09-28 20:27:51 +000037\begin{funcdesc}{popen4}{cmd\optional{, bufsize\optional{, mode}}}
38Executes \var{cmd} as a sub-process. Returns the file objects
39\code{(\var{child_stdout_and_stderr}, \var{child_stdin})}.
40\versionadded{2.0}
41\end{funcdesc}
42
43
44On \UNIX, a class defining the objects returned by the factory
45functions is also available. These are not used for the Windows
46implementation, and are not available on that platform.
Fred Drake6afad371998-04-28 14:28:15 +000047
48\begin{classdesc}{Popen3}{cmd\optional{, capturestderr\optional{, bufsize}}}
49This class represents a child process. Normally, \class{Popen3}
Fred Drake8a9db992000-09-28 20:27:51 +000050instances are created using the \function{popen2()} and
51\function{popen3()} factory functions described above.
Fred Drake6afad371998-04-28 14:28:15 +000052
53If not using one off the helper functions to create \class{Popen3}
54objects, the parameter \var{cmd} is the shell command to execute in a
55sub-process. The \var{capturestderr} flag, if true, specifies that
56the object should capture standard error output of the child process.
57The default is false. If the \var{bufsize} parameter is specified, it
58specifies the size of the I/O buffers to/from the child process.
59\end{classdesc}
60
Fred Drake8a9db992000-09-28 20:27:51 +000061\begin{classdesc}{Popen4}{cmd\optional{, bufsize}}
62Similar to \class{Popen3}, but always captures standard error into the
63same file object as standard output. These are typically created
64using \function{popen4()}.
65\versionadded{2.0}
66\end{classdesc}
Fred Drake6afad371998-04-28 14:28:15 +000067
Fred Drake6afad371998-04-28 14:28:15 +000068
Fred Drake8a9db992000-09-28 20:27:51 +000069\subsection{Popen3 and Popen4 Objects \label{popen3-objects}}
70
71Instances of the \class{Popen3} and \class{Popen4} classes have the
72following methods:
Fred Drake6afad371998-04-28 14:28:15 +000073
74\begin{methoddesc}{poll}{}
75Returns \code{-1} if child process hasn't completed yet, or its return
76code otherwise.
77\end{methoddesc}
78
79\begin{methoddesc}{wait}{}
Fred Drake45c23e62001-07-06 17:17:12 +000080Waits for and returns the status code of the child process. The
81status code encodes both the return code of the process and
82information about whether it exited using the \cfunction{exit()}
83system call or died due to a signal. Functions to help interpret the
84status code are defined in the \refmodule{os} module; see section
85\ref{os-process} for the \function{W\var{*}()} family of functions.
Fred Drake6afad371998-04-28 14:28:15 +000086\end{methoddesc}
87
88
Fred Drake8a9db992000-09-28 20:27:51 +000089The following attributes are also available:
Fred Drake6afad371998-04-28 14:28:15 +000090
Fred Drake3aa70d61999-05-27 17:50:59 +000091\begin{memberdesc}{fromchild}
Fred Drake8a9db992000-09-28 20:27:51 +000092A file object that provides output from the child process. For
93\class{Popen4} instances, this will provide both the standard output
94and standard error streams.
Fred Drake3aa70d61999-05-27 17:50:59 +000095\end{memberdesc}
Fred Drake6afad371998-04-28 14:28:15 +000096
Fred Drake3aa70d61999-05-27 17:50:59 +000097\begin{memberdesc}{tochild}
Fred Drake6afad371998-04-28 14:28:15 +000098A file object that provides input to the child process.
Fred Drake3aa70d61999-05-27 17:50:59 +000099\end{memberdesc}
Fred Drake6afad371998-04-28 14:28:15 +0000100
Fred Drake3aa70d61999-05-27 17:50:59 +0000101\begin{memberdesc}{childerr}
Fred Drake6afad371998-04-28 14:28:15 +0000102Where the standard error from the child process goes is
103\var{capturestderr} was true for the constructor, or \code{None}.
Fred Drake8a9db992000-09-28 20:27:51 +0000104This will always be \code{None} for \class{Popen4} instances.
Fred Drake3aa70d61999-05-27 17:50:59 +0000105\end{memberdesc}
106
107\begin{memberdesc}{pid}
108The process ID of the child process.
109\end{memberdesc}