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