1998-08-10 16:42:37 -03:00
|
|
|
|
\section{\module{popen2} ---
|
1999-05-27 14:50:59 -03:00
|
|
|
|
Subprocesses with accessible I/O streams}
|
1998-08-10 16:42:37 -03:00
|
|
|
|
|
1999-03-12 12:24:22 -04:00
|
|
|
|
\declaremodule{standard}{popen2}
|
1998-07-27 19:20:02 -03:00
|
|
|
|
\modulesynopsis{Subprocesses with accessible standard I/O streams.}
|
1999-03-02 12:37:17 -04:00
|
|
|
|
\sectionauthor{Drew Csillag}{drew_csillag@geocities.com}
|
1998-07-23 14:59:49 -03:00
|
|
|
|
|
1998-04-28 11:28:15 -03:00
|
|
|
|
|
2000-09-28 17:27:51 -03:00
|
|
|
|
This module allows you to spawn processes and connect to their
|
|
|
|
|
input/output/error pipes and obtain their return codes under
|
|
|
|
|
\UNIX{} and Windows.
|
1998-04-28 11:28:15 -03:00
|
|
|
|
|
2006-10-27 11:53:41 -03:00
|
|
|
|
The \module{subprocess} module provides more powerful facilities for
|
|
|
|
|
spawning new processes and retrieving their results. Using the
|
|
|
|
|
\module{subprocess} module is preferable to using the \module{popen2}
|
|
|
|
|
module.
|
1998-04-28 11:28:15 -03:00
|
|
|
|
|
2000-09-28 17:27:51 -03:00
|
|
|
|
The primary interface offered by this module is a trio of factory
|
|
|
|
|
functions. For each of these, if \var{bufsize} is specified,
|
|
|
|
|
it specifies the buffer size for the I/O pipes. \var{mode}, if
|
|
|
|
|
provided, should be the string \code{'b'} or \code{'t'}; on Windows
|
|
|
|
|
this is needed to determine whether the file objects should be opened
|
|
|
|
|
in binary or text mode. The default value for \var{mode} is
|
|
|
|
|
\code{'t'}.
|
|
|
|
|
|
2004-10-11 15:12:20 -03:00
|
|
|
|
On \UNIX, \var{cmd} may be a sequence, in which case arguments will be passed
|
|
|
|
|
directly to the program without shell intervention (as with
|
|
|
|
|
\function{os.spawnv()}). If \var{cmd} is a string it will be passed to the
|
|
|
|
|
shell (as with \function{os.system()}).
|
|
|
|
|
|
2001-09-11 16:56:51 -03:00
|
|
|
|
The only way to retrieve the return codes for the child processes is
|
|
|
|
|
by using the \method{poll()} or \method{wait()} methods on the
|
|
|
|
|
\class{Popen3} and \class{Popen4} classes; these are only available on
|
|
|
|
|
\UNIX. This information is not available when using the
|
|
|
|
|
\function{popen2()}, \function{popen3()}, and \function{popen4()}
|
|
|
|
|
functions, or the equivalent functions in the \refmodule{os} module.
|
2004-08-09 11:12:05 -03:00
|
|
|
|
(Note that the tuples returned by the \refmodule{os} module's functions
|
|
|
|
|
are in a different order from the ones returned by the \module{popen2}
|
|
|
|
|
module.)
|
2001-09-11 16:56:51 -03:00
|
|
|
|
|
2000-09-28 17:27:51 -03:00
|
|
|
|
\begin{funcdesc}{popen2}{cmd\optional{, bufsize\optional{, mode}}}
|
|
|
|
|
Executes \var{cmd} as a sub-process. Returns the file objects
|
|
|
|
|
\code{(\var{child_stdout}, \var{child_stdin})}.
|
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
|
|
|
|
\begin{funcdesc}{popen3}{cmd\optional{, bufsize\optional{, mode}}}
|
|
|
|
|
Executes \var{cmd} as a sub-process. Returns the file objects
|
|
|
|
|
\code{(\var{child_stdout}, \var{child_stdin}, \var{child_stderr})}.
|
1998-04-28 11:28:15 -03:00
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
2000-09-28 17:27:51 -03:00
|
|
|
|
\begin{funcdesc}{popen4}{cmd\optional{, bufsize\optional{, mode}}}
|
|
|
|
|
Executes \var{cmd} as a sub-process. Returns the file objects
|
|
|
|
|
\code{(\var{child_stdout_and_stderr}, \var{child_stdin})}.
|
|
|
|
|
\versionadded{2.0}
|
1998-04-28 11:28:15 -03:00
|
|
|
|
\end{funcdesc}
|
|
|
|
|
|
2000-09-28 17:27:51 -03:00
|
|
|
|
|
|
|
|
|
On \UNIX, a class defining the objects returned by the factory
|
|
|
|
|
functions is also available. These are not used for the Windows
|
|
|
|
|
implementation, and are not available on that platform.
|
1998-04-28 11:28:15 -03:00
|
|
|
|
|
|
|
|
|
\begin{classdesc}{Popen3}{cmd\optional{, capturestderr\optional{, bufsize}}}
|
|
|
|
|
This class represents a child process. Normally, \class{Popen3}
|
2000-09-28 17:27:51 -03:00
|
|
|
|
instances are created using the \function{popen2()} and
|
|
|
|
|
\function{popen3()} factory functions described above.
|
1998-04-28 11:28:15 -03:00
|
|
|
|
|
2003-02-06 14:04:43 -04:00
|
|
|
|
If not using one of the helper functions to create \class{Popen3}
|
1998-04-28 11:28:15 -03:00
|
|
|
|
objects, the parameter \var{cmd} is the shell command to execute in a
|
|
|
|
|
sub-process. The \var{capturestderr} flag, if true, specifies that
|
|
|
|
|
the object should capture standard error output of the child process.
|
|
|
|
|
The default is false. If the \var{bufsize} parameter is specified, it
|
|
|
|
|
specifies the size of the I/O buffers to/from the child process.
|
|
|
|
|
\end{classdesc}
|
|
|
|
|
|
2000-09-28 17:27:51 -03:00
|
|
|
|
\begin{classdesc}{Popen4}{cmd\optional{, bufsize}}
|
|
|
|
|
Similar to \class{Popen3}, but always captures standard error into the
|
|
|
|
|
same file object as standard output. These are typically created
|
|
|
|
|
using \function{popen4()}.
|
|
|
|
|
\versionadded{2.0}
|
|
|
|
|
\end{classdesc}
|
|
|
|
|
|
|
|
|
|
\subsection{Popen3 and Popen4 Objects \label{popen3-objects}}
|
1998-04-28 11:28:15 -03:00
|
|
|
|
|
2000-09-28 17:27:51 -03:00
|
|
|
|
Instances of the \class{Popen3} and \class{Popen4} classes have the
|
|
|
|
|
following methods:
|
1998-04-28 11:28:15 -03:00
|
|
|
|
|
|
|
|
|
\begin{methoddesc}{poll}{}
|
|
|
|
|
Returns \code{-1} if child process hasn't completed yet, or its return
|
|
|
|
|
code otherwise.
|
|
|
|
|
\end{methoddesc}
|
|
|
|
|
|
|
|
|
|
\begin{methoddesc}{wait}{}
|
2001-07-06 14:17:12 -03:00
|
|
|
|
Waits for and returns the status code of the child process. The
|
|
|
|
|
status code encodes both the return code of the process and
|
|
|
|
|
information about whether it exited using the \cfunction{exit()}
|
|
|
|
|
system call or died due to a signal. Functions to help interpret the
|
|
|
|
|
status code are defined in the \refmodule{os} module; see section
|
|
|
|
|
\ref{os-process} for the \function{W\var{*}()} family of functions.
|
1998-04-28 11:28:15 -03:00
|
|
|
|
\end{methoddesc}
|
|
|
|
|
|
|
|
|
|
|
2000-09-28 17:27:51 -03:00
|
|
|
|
The following attributes are also available:
|
1998-04-28 11:28:15 -03:00
|
|
|
|
|
1999-05-27 14:50:59 -03:00
|
|
|
|
\begin{memberdesc}{fromchild}
|
2000-09-28 17:27:51 -03:00
|
|
|
|
A file object that provides output from the child process. For
|
|
|
|
|
\class{Popen4} instances, this will provide both the standard output
|
|
|
|
|
and standard error streams.
|
1999-05-27 14:50:59 -03:00
|
|
|
|
\end{memberdesc}
|
1998-04-28 11:28:15 -03:00
|
|
|
|
|
1999-05-27 14:50:59 -03:00
|
|
|
|
\begin{memberdesc}{tochild}
|
1998-04-28 11:28:15 -03:00
|
|
|
|
A file object that provides input to the child process.
|
1999-05-27 14:50:59 -03:00
|
|
|
|
\end{memberdesc}
|
1998-04-28 11:28:15 -03:00
|
|
|
|
|
1999-05-27 14:50:59 -03:00
|
|
|
|
\begin{memberdesc}{childerr}
|
2003-12-23 13:01:38 -04:00
|
|
|
|
A file object that provides error output from the child process, if
|
|
|
|
|
\var{capturestderr} was true for the constructor, otherwise
|
|
|
|
|
\code{None}. This will always be \code{None} for \class{Popen4}
|
|
|
|
|
instances.
|
1999-05-27 14:50:59 -03:00
|
|
|
|
\end{memberdesc}
|
|
|
|
|
|
|
|
|
|
\begin{memberdesc}{pid}
|
|
|
|
|
The process ID of the child process.
|
|
|
|
|
\end{memberdesc}
|
2002-06-18 17:30:37 -03:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
\subsection{Flow Control Issues \label{popen2-flow-control}}
|
|
|
|
|
|
|
|
|
|
Any time you are working with any form of inter-process communication,
|
|
|
|
|
control flow needs to be carefully thought out. This remains the case
|
|
|
|
|
with the file objects provided by this module (or the \refmodule{os}
|
|
|
|
|
module equivalents).
|
|
|
|
|
|
|
|
|
|
% Example explanation and suggested work-arounds substantially stolen
|
|
|
|
|
% from Martin von L<>wis:
|
|
|
|
|
% http://mail.python.org/pipermail/python-dev/2000-September/009460.html
|
|
|
|
|
|
|
|
|
|
When reading output from a child process that writes a lot of data to
|
|
|
|
|
standard error while the parent is reading from the child's standard
|
2003-02-06 14:04:43 -04:00
|
|
|
|
output, a deadlock can occur. A similar situation can occur with other
|
2002-06-18 17:30:37 -03:00
|
|
|
|
combinations of reads and writes. The essential factors are that more
|
2002-06-18 17:38:05 -03:00
|
|
|
|
than \constant{_PC_PIPE_BUF} bytes are being written by one process in
|
2002-06-18 17:30:37 -03:00
|
|
|
|
a blocking fashion, while the other process is reading from the other
|
|
|
|
|
process, also in a blocking fashion.
|
|
|
|
|
|
|
|
|
|
There are several ways to deal with this situation.
|
|
|
|
|
|
|
|
|
|
The simplest application change, in many cases, will be to follow this
|
|
|
|
|
model in the parent process:
|
|
|
|
|
|
|
|
|
|
\begin{verbatim}
|
|
|
|
|
import popen2
|
|
|
|
|
|
|
|
|
|
r, w, e = popen2.popen3('python slave.py')
|
|
|
|
|
e.readlines()
|
|
|
|
|
r.readlines()
|
|
|
|
|
r.close()
|
|
|
|
|
e.close()
|
|
|
|
|
w.close()
|
|
|
|
|
\end{verbatim}
|
|
|
|
|
|
|
|
|
|
with code like this in the child:
|
|
|
|
|
|
|
|
|
|
\begin{verbatim}
|
|
|
|
|
import os
|
|
|
|
|
import sys
|
|
|
|
|
|
|
|
|
|
# note that each of these print statements
|
|
|
|
|
# writes a single long string
|
|
|
|
|
|
|
|
|
|
print >>sys.stderr, 400 * 'this is a test\n'
|
|
|
|
|
os.close(sys.stderr.fileno())
|
|
|
|
|
print >>sys.stdout, 400 * 'this is another test\n'
|
|
|
|
|
\end{verbatim}
|
|
|
|
|
|
|
|
|
|
In particular, note that \code{sys.stderr} must be closed after
|
|
|
|
|
writing all data, or \method{readlines()} won't return. Also note
|
|
|
|
|
that \function{os.close()} must be used, as \code{sys.stderr.close()}
|
|
|
|
|
won't close \code{stderr} (otherwise assigning to \code{sys.stderr}
|
|
|
|
|
will silently close it, so no further errors can be printed).
|
|
|
|
|
|
|
|
|
|
Applications which need to support a more general approach should
|
|
|
|
|
integrate I/O over pipes with their \function{select()} loops, or use
|
|
|
|
|
separate threads to read each of the individual files provided by
|
|
|
|
|
whichever \function{popen*()} function or \class{Popen*} class was
|
|
|
|
|
used.
|
2006-10-27 11:53:41 -03:00
|
|
|
|
|
|
|
|
|
\begin{seealso}
|
|
|
|
|
\seemodule{subprocess}{Module for spawning and managing subprocesses.}
|
|
|
|
|
\end{seealso}
|