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 | 5fac2ab | 1999-06-01 17:55:52 +0000 | [diff] [blame] | 5 | \platform{Unix} |
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 | |
| 10 | This module allows you to spawn processes and connect their |
Fred Drake | 5fac2ab | 1999-06-01 17:55:52 +0000 | [diff] [blame] | 11 | input/output/error pipes and obtain their return codes under \UNIX. |
| 12 | Similar functionality exists for Windows platforms using the |
| 13 | \module{win32pipe} module provided as part of Mark Hammond's Windows |
| 14 | extensions. |
Fred Drake | 6afad37 | 1998-04-28 14:28:15 +0000 | [diff] [blame] | 15 | |
| 16 | The primary interface offered by this module is a pair of factory |
| 17 | functions: |
| 18 | |
| 19 | \begin{funcdesc}{popen2}{cmd\optional{, bufsize}} |
| 20 | Executes \var{cmd} as a sub-process. If \var{bufsize} is specified, |
Fred Drake | 7eba1d8 | 1999-08-19 17:00:38 +0000 | [diff] [blame] | 21 | it specifies the buffer size for the I/O pipes. Returns the file |
| 22 | objects \code{(\var{child_stdout}, \var{child_stdin})}. |
Fred Drake | 6afad37 | 1998-04-28 14:28:15 +0000 | [diff] [blame] | 23 | \end{funcdesc} |
| 24 | |
Guido van Rossum | 3a5a02f | 1998-08-13 01:27:01 +0000 | [diff] [blame] | 25 | \begin{funcdesc}{popen3}{cmd\optional{, bufsize}} |
Fred Drake | 6afad37 | 1998-04-28 14:28:15 +0000 | [diff] [blame] | 26 | Executes \var{cmd} as a sub-process. If \var{bufsize} is specified, |
Fred Drake | 7eba1d8 | 1999-08-19 17:00:38 +0000 | [diff] [blame] | 27 | it specifies the buffer size for the I/O pipes. Returns the file |
| 28 | objects \code{(\var{child_stdout}, \var{child_stdin}, |
| 29 | \var{child_stderr})}. |
Fred Drake | 6afad37 | 1998-04-28 14:28:15 +0000 | [diff] [blame] | 30 | \end{funcdesc} |
| 31 | |
| 32 | The class defining the objects returned by the factory functions is |
| 33 | also available: |
| 34 | |
| 35 | \begin{classdesc}{Popen3}{cmd\optional{, capturestderr\optional{, bufsize}}} |
| 36 | This class represents a child process. Normally, \class{Popen3} |
| 37 | instances are created using the factory functions described above. |
| 38 | |
| 39 | If not using one off the helper functions to create \class{Popen3} |
| 40 | objects, the parameter \var{cmd} is the shell command to execute in a |
| 41 | sub-process. The \var{capturestderr} flag, if true, specifies that |
| 42 | the object should capture standard error output of the child process. |
| 43 | The default is false. If the \var{bufsize} parameter is specified, it |
| 44 | specifies the size of the I/O buffers to/from the child process. |
| 45 | \end{classdesc} |
| 46 | |
| 47 | |
Fred Drake | 3aa70d6 | 1999-05-27 17:50:59 +0000 | [diff] [blame] | 48 | \subsection{Popen3 Objects \label{popen3-objects}} |
Fred Drake | 6afad37 | 1998-04-28 14:28:15 +0000 | [diff] [blame] | 49 | |
| 50 | Instances of the \class{Popen3} class have the following methods: |
| 51 | |
| 52 | \begin{methoddesc}{poll}{} |
| 53 | Returns \code{-1} if child process hasn't completed yet, or its return |
| 54 | code otherwise. |
| 55 | \end{methoddesc} |
| 56 | |
| 57 | \begin{methoddesc}{wait}{} |
| 58 | Waits for and returns the return code of the child process. |
| 59 | \end{methoddesc} |
| 60 | |
| 61 | |
| 62 | The following attributes of \class{Popen3} objects are also available: |
| 63 | |
Fred Drake | 3aa70d6 | 1999-05-27 17:50:59 +0000 | [diff] [blame] | 64 | \begin{memberdesc}{fromchild} |
Fred Drake | 6afad37 | 1998-04-28 14:28:15 +0000 | [diff] [blame] | 65 | A file object that provides output from the child process. |
Fred Drake | 3aa70d6 | 1999-05-27 17:50:59 +0000 | [diff] [blame] | 66 | \end{memberdesc} |
Fred Drake | 6afad37 | 1998-04-28 14:28:15 +0000 | [diff] [blame] | 67 | |
Fred Drake | 3aa70d6 | 1999-05-27 17:50:59 +0000 | [diff] [blame] | 68 | \begin{memberdesc}{tochild} |
Fred Drake | 6afad37 | 1998-04-28 14:28:15 +0000 | [diff] [blame] | 69 | A file object that provides input to the child process. |
Fred Drake | 3aa70d6 | 1999-05-27 17:50:59 +0000 | [diff] [blame] | 70 | \end{memberdesc} |
Fred Drake | 6afad37 | 1998-04-28 14:28:15 +0000 | [diff] [blame] | 71 | |
Fred Drake | 3aa70d6 | 1999-05-27 17:50:59 +0000 | [diff] [blame] | 72 | \begin{memberdesc}{childerr} |
Fred Drake | 6afad37 | 1998-04-28 14:28:15 +0000 | [diff] [blame] | 73 | Where the standard error from the child process goes is |
| 74 | \var{capturestderr} was true for the constructor, or \code{None}. |
Fred Drake | 3aa70d6 | 1999-05-27 17:50:59 +0000 | [diff] [blame] | 75 | \end{memberdesc} |
| 76 | |
| 77 | \begin{memberdesc}{pid} |
| 78 | The process ID of the child process. |
| 79 | \end{memberdesc} |