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 | |
| 27 | \begin{funcdesc}{popen2}{cmd\optional{, bufsize\optional{, mode}}} |
| 28 | Executes \var{cmd} as a sub-process. Returns the file objects |
| 29 | \code{(\var{child_stdout}, \var{child_stdin})}. |
Fred Drake | 6afad37 | 1998-04-28 14:28:15 +0000 | [diff] [blame] | 30 | \end{funcdesc} |
| 31 | |
Fred Drake | 8a9db99 | 2000-09-28 20:27:51 +0000 | [diff] [blame] | 32 | \begin{funcdesc}{popen3}{cmd\optional{, bufsize\optional{, mode}}} |
| 33 | Executes \var{cmd} as a sub-process. Returns the file objects |
| 34 | \code{(\var{child_stdout}, \var{child_stdin}, \var{child_stderr})}. |
Fred Drake | 6afad37 | 1998-04-28 14:28:15 +0000 | [diff] [blame] | 35 | \end{funcdesc} |
| 36 | |
Fred Drake | 8a9db99 | 2000-09-28 20:27:51 +0000 | [diff] [blame] | 37 | \begin{funcdesc}{popen4}{cmd\optional{, bufsize\optional{, mode}}} |
| 38 | Executes \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 | |
| 44 | On \UNIX, a class defining the objects returned by the factory |
| 45 | functions is also available. These are not used for the Windows |
| 46 | implementation, and are not available on that platform. |
Fred Drake | 6afad37 | 1998-04-28 14:28:15 +0000 | [diff] [blame] | 47 | |
| 48 | \begin{classdesc}{Popen3}{cmd\optional{, capturestderr\optional{, bufsize}}} |
| 49 | This class represents a child process. Normally, \class{Popen3} |
Fred Drake | 8a9db99 | 2000-09-28 20:27:51 +0000 | [diff] [blame] | 50 | instances are created using the \function{popen2()} and |
| 51 | \function{popen3()} factory functions described above. |
Fred Drake | 6afad37 | 1998-04-28 14:28:15 +0000 | [diff] [blame] | 52 | |
| 53 | If not using one off the helper functions to create \class{Popen3} |
| 54 | objects, the parameter \var{cmd} is the shell command to execute in a |
| 55 | sub-process. The \var{capturestderr} flag, if true, specifies that |
| 56 | the object should capture standard error output of the child process. |
| 57 | The default is false. If the \var{bufsize} parameter is specified, it |
| 58 | specifies the size of the I/O buffers to/from the child process. |
| 59 | \end{classdesc} |
| 60 | |
Fred Drake | 8a9db99 | 2000-09-28 20:27:51 +0000 | [diff] [blame] | 61 | \begin{classdesc}{Popen4}{cmd\optional{, bufsize}} |
| 62 | Similar to \class{Popen3}, but always captures standard error into the |
| 63 | same file object as standard output. These are typically created |
| 64 | using \function{popen4()}. |
| 65 | \versionadded{2.0} |
| 66 | \end{classdesc} |
Fred Drake | 6afad37 | 1998-04-28 14:28:15 +0000 | [diff] [blame] | 67 | |
Fred Drake | 6afad37 | 1998-04-28 14:28:15 +0000 | [diff] [blame] | 68 | |
Fred Drake | 8a9db99 | 2000-09-28 20:27:51 +0000 | [diff] [blame] | 69 | \subsection{Popen3 and Popen4 Objects \label{popen3-objects}} |
| 70 | |
| 71 | Instances of the \class{Popen3} and \class{Popen4} classes have the |
| 72 | following methods: |
Fred Drake | 6afad37 | 1998-04-28 14:28:15 +0000 | [diff] [blame] | 73 | |
| 74 | \begin{methoddesc}{poll}{} |
| 75 | Returns \code{-1} if child process hasn't completed yet, or its return |
| 76 | code otherwise. |
| 77 | \end{methoddesc} |
| 78 | |
| 79 | \begin{methoddesc}{wait}{} |
| 80 | Waits for and returns the return code of the child process. |
| 81 | \end{methoddesc} |
| 82 | |
| 83 | |
Fred Drake | 8a9db99 | 2000-09-28 20:27:51 +0000 | [diff] [blame] | 84 | The following attributes are also available: |
Fred Drake | 6afad37 | 1998-04-28 14:28:15 +0000 | [diff] [blame] | 85 | |
Fred Drake | 3aa70d6 | 1999-05-27 17:50:59 +0000 | [diff] [blame] | 86 | \begin{memberdesc}{fromchild} |
Fred Drake | 8a9db99 | 2000-09-28 20:27:51 +0000 | [diff] [blame] | 87 | A file object that provides output from the child process. For |
| 88 | \class{Popen4} instances, this will provide both the standard output |
| 89 | and standard error streams. |
Fred Drake | 3aa70d6 | 1999-05-27 17:50:59 +0000 | [diff] [blame] | 90 | \end{memberdesc} |
Fred Drake | 6afad37 | 1998-04-28 14:28:15 +0000 | [diff] [blame] | 91 | |
Fred Drake | 3aa70d6 | 1999-05-27 17:50:59 +0000 | [diff] [blame] | 92 | \begin{memberdesc}{tochild} |
Fred Drake | 6afad37 | 1998-04-28 14:28:15 +0000 | [diff] [blame] | 93 | A file object that provides input to the child process. |
Fred Drake | 3aa70d6 | 1999-05-27 17:50:59 +0000 | [diff] [blame] | 94 | \end{memberdesc} |
Fred Drake | 6afad37 | 1998-04-28 14:28:15 +0000 | [diff] [blame] | 95 | |
Fred Drake | 3aa70d6 | 1999-05-27 17:50:59 +0000 | [diff] [blame] | 96 | \begin{memberdesc}{childerr} |
Fred Drake | 6afad37 | 1998-04-28 14:28:15 +0000 | [diff] [blame] | 97 | Where the standard error from the child process goes is |
| 98 | \var{capturestderr} was true for the constructor, or \code{None}. |
Fred Drake | 8a9db99 | 2000-09-28 20:27:51 +0000 | [diff] [blame] | 99 | This will always be \code{None} for \class{Popen4} instances. |
Fred Drake | 3aa70d6 | 1999-05-27 17:50:59 +0000 | [diff] [blame] | 100 | \end{memberdesc} |
| 101 | |
| 102 | \begin{memberdesc}{pid} |
| 103 | The process ID of the child process. |
| 104 | \end{memberdesc} |