blob: 26351759a2d90be6b83e2f7878ad7b19571f8ead [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001
2:mod:`popen2` --- Subprocesses with accessible I/O streams
3==========================================================
4
5.. module:: popen2
6 :synopsis: Subprocesses with accessible standard I/O streams.
Georg Brandl7f758c42007-08-15 18:41:25 +00007 :deprecated:
Georg Brandl8ec7f652007-08-15 14:28:01 +00008.. sectionauthor:: Drew Csillag <drew_csillag@geocities.com>
9
10
11.. deprecated:: 2.6
12 This module is obsolete. Use the :mod:`subprocess` module.
13
14This module allows you to spawn processes and connect to their
15input/output/error pipes and obtain their return codes under Unix and Windows.
16
17The :mod:`subprocess` module provides more powerful facilities for spawning new
18processes and retrieving their results. Using the :mod:`subprocess` module is
19preferable to using the :mod:`popen2` module.
20
21The primary interface offered by this module is a trio of factory functions.
22For each of these, if *bufsize* is specified, it specifies the buffer size for
23the I/O pipes. *mode*, if provided, should be the string ``'b'`` or ``'t'``; on
24Windows this is needed to determine whether the file objects should be opened in
25binary or text mode. The default value for *mode* is ``'t'``.
26
27On Unix, *cmd* may be a sequence, in which case arguments will be passed
28directly to the program without shell intervention (as with :func:`os.spawnv`).
29If *cmd* is a string it will be passed to the shell (as with :func:`os.system`).
30
31The only way to retrieve the return codes for the child processes is by using
32the :meth:`poll` or :meth:`wait` methods on the :class:`Popen3` and
33:class:`Popen4` classes; these are only available on Unix. This information is
34not available when using the :func:`popen2`, :func:`popen3`, and :func:`popen4`
35functions, or the equivalent functions in the :mod:`os` module. (Note that the
36tuples returned by the :mod:`os` module's functions are in a different order
37from the ones returned by the :mod:`popen2` module.)
38
39
40.. function:: popen2(cmd[, bufsize[, mode]])
41
42 Executes *cmd* as a sub-process. Returns the file objects ``(child_stdout,
43 child_stdin)``.
44
45
46.. function:: popen3(cmd[, bufsize[, mode]])
47
48 Executes *cmd* as a sub-process. Returns the file objects ``(child_stdout,
49 child_stdin, child_stderr)``.
50
51
52.. function:: popen4(cmd[, bufsize[, mode]])
53
54 Executes *cmd* as a sub-process. Returns the file objects
55 ``(child_stdout_and_stderr, child_stdin)``.
56
57 .. versionadded:: 2.0
58
59On Unix, a class defining the objects returned by the factory functions is also
60available. These are not used for the Windows implementation, and are not
61available on that platform.
62
63
64.. class:: Popen3(cmd[, capturestderr[, bufsize]])
65
66 This class represents a child process. Normally, :class:`Popen3` instances are
67 created using the :func:`popen2` and :func:`popen3` factory functions described
68 above.
69
70 If not using one of the helper functions to create :class:`Popen3` objects, the
71 parameter *cmd* is the shell command to execute in a sub-process. The
72 *capturestderr* flag, if true, specifies that the object should capture standard
73 error output of the child process. The default is false. If the *bufsize*
74 parameter is specified, it specifies the size of the I/O buffers to/from the
75 child process.
76
77
78.. class:: Popen4(cmd[, bufsize])
79
80 Similar to :class:`Popen3`, but always captures standard error into the same
81 file object as standard output. These are typically created using
82 :func:`popen4`.
83
84 .. versionadded:: 2.0
85
86
87.. _popen3-objects:
88
89Popen3 and Popen4 Objects
90-------------------------
91
92Instances of the :class:`Popen3` and :class:`Popen4` classes have the following
93methods:
94
95
96.. method:: Popen3.poll()
97
Georg Brandl7cf40792007-08-23 17:57:05 +000098 Returns ``-1`` if child process hasn't completed yet, or its status code
99 (see :meth:`wait`) otherwise.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000100
101
102.. method:: Popen3.wait()
103
104 Waits for and returns the status code of the child process. The status code
105 encodes both the return code of the process and information about whether it
106 exited using the :cfunc:`exit` system call or died due to a signal. Functions
107 to help interpret the status code are defined in the :mod:`os` module; see
108 section :ref:`os-process` for the :func:`W\*` family of functions.
109
110The following attributes are also available:
111
112
113.. attribute:: Popen3.fromchild
114
115 A file object that provides output from the child process. For :class:`Popen4`
116 instances, this will provide both the standard output and standard error
117 streams.
118
119
120.. attribute:: Popen3.tochild
121
122 A file object that provides input to the child process.
123
124
125.. attribute:: Popen3.childerr
126
127 A file object that provides error output from the child process, if
128 *capturestderr* was true for the constructor, otherwise ``None``. This will
129 always be ``None`` for :class:`Popen4` instances.
130
131
132.. attribute:: Popen3.pid
133
134 The process ID of the child process.
135
136
137.. _popen2-flow-control:
138
139Flow Control Issues
140-------------------
141
142Any time you are working with any form of inter-process communication, control
143flow needs to be carefully thought out. This remains the case with the file
144objects provided by this module (or the :mod:`os` module equivalents).
145
146When reading output from a child process that writes a lot of data to standard
147error while the parent is reading from the child's standard output, a deadlock
148can occur. A similar situation can occur with other combinations of reads and
149writes. The essential factors are that more than :const:`_PC_PIPE_BUF` bytes
150are being written by one process in a blocking fashion, while the other process
151is reading from the other process, also in a blocking fashion.
152
Georg Brandlb19be572007-12-29 10:57:00 +0000153.. Example explanation and suggested work-arounds substantially stolen
154 from Martin von Löwis:
155 http://mail.python.org/pipermail/python-dev/2000-September/009460.html
Georg Brandl8ec7f652007-08-15 14:28:01 +0000156
157There are several ways to deal with this situation.
158
159The simplest application change, in many cases, will be to follow this model in
160the parent process::
161
162 import popen2
163
164 r, w, e = popen2.popen3('python slave.py')
165 e.readlines()
166 r.readlines()
167 r.close()
168 e.close()
169 w.close()
170
171with code like this in the child::
172
173 import os
174 import sys
175
176 # note that each of these print statements
177 # writes a single long string
178
179 print >>sys.stderr, 400 * 'this is a test\n'
180 os.close(sys.stderr.fileno())
181 print >>sys.stdout, 400 * 'this is another test\n'
182
183In particular, note that ``sys.stderr`` must be closed after writing all data,
184or :meth:`readlines` won't return. Also note that :func:`os.close` must be
185used, as ``sys.stderr.close()`` won't close ``stderr`` (otherwise assigning to
186``sys.stderr`` will silently close it, so no further errors can be printed).
187
188Applications which need to support a more general approach should integrate I/O
189over pipes with their :func:`select` loops, or use separate threads to read each
190of the individual files provided by whichever :func:`popen\*` function or
191:class:`Popen\*` class was used.
192
193
194.. seealso::
195
196 Module :mod:`subprocess`
197 Module for spawning and managing subprocesses.
198