Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> | |
5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> | |
6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> | |
7 | <link rel="first" href="lib.html" title='Python Library Reference' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='index' href='genindex.html' title='Index' /> | |
10 | <link rel='last' href='about.html' title='About this document...' /> | |
11 | <link rel='help' href='about.html' title='About this document...' /> | |
12 | <link rel="prev" href="popen3-objects.html" /> | |
13 | <link rel="parent" href="module-popen2.html" /> | |
14 | <link rel="next" href="module-datetime.html" /> | |
15 | <meta name='aesop' content='information' /> | |
16 | <title>6.9.2 Flow Control Issues </title> | |
17 | </head> | |
18 | <body> | |
19 | <DIV CLASS="navigation"> | |
20 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
21 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
22 | <tr> | |
23 | <td class='online-navigation'><a rel="prev" title="6.9.1 Popen3 and Popen4" | |
24 | href="popen3-objects.html"><img src='../icons/previous.png' | |
25 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
26 | <td class='online-navigation'><a rel="parent" title="6.9 popen2 " | |
27 | href="module-popen2.html"><img src='../icons/up.png' | |
28 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
29 | <td class='online-navigation'><a rel="next" title="6.10 datetime " | |
30 | href="module-datetime.html"><img src='../icons/next.png' | |
31 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
32 | <td align="center" width="100%">Python Library Reference</td> | |
33 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
34 | href="contents.html"><img src='../icons/contents.png' | |
35 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
36 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
37 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
38 | <td class='online-navigation'><a rel="index" title="Index" | |
39 | href="genindex.html"><img src='../icons/index.png' | |
40 | border='0' height='32' alt='Index' width='32' /></A></td> | |
41 | </tr></table> | |
42 | <div class='online-navigation'> | |
43 | <b class="navlabel">Previous:</b> | |
44 | <a class="sectref" rel="prev" href="popen3-objects.html">6.9.1 Popen3 and Popen4</A> | |
45 | <b class="navlabel">Up:</b> | |
46 | <a class="sectref" rel="parent" href="module-popen2.html">6.9 popen2 </A> | |
47 | <b class="navlabel">Next:</b> | |
48 | <a class="sectref" rel="next" href="module-datetime.html">6.10 datetime </A> | |
49 | </div> | |
50 | <hr /></div> | |
51 | </DIV> | |
52 | <!--End of Navigation Panel--> | |
53 | ||
54 | <H2><A NAME="SECTION008920000000000000000"></A><A NAME="popen2-flow-control"></A> | |
55 | <BR> | |
56 | 6.9.2 Flow Control Issues | |
57 | </H2> | |
58 | ||
59 | <P> | |
60 | Any time you are working with any form of inter-process communication, | |
61 | control flow needs to be carefully thought out. This remains the case | |
62 | with the file objects provided by this module (or the <tt class="module"><a href="module-os.html">os</a></tt> | |
63 | module equivalents). | |
64 | ||
65 | <P> | |
66 | When reading output from a child process that writes a lot of data to | |
67 | standard error while the parent is reading from the child's standard | |
68 | output, a deadlock can occur. A similar situation can occur with other | |
69 | combinations of reads and writes. The essential factors are that more | |
70 | than <tt class="constant">_PC_PIPE_BUF</tt> bytes are being written by one process in | |
71 | a blocking fashion, while the other process is reading from the other | |
72 | process, also in a blocking fashion. | |
73 | ||
74 | <P> | |
75 | There are several ways to deal with this situation. | |
76 | ||
77 | <P> | |
78 | The simplest application change, in many cases, will be to follow this | |
79 | model in the parent process: | |
80 | ||
81 | <P> | |
82 | <div class="verbatim"><pre> | |
83 | import popen2 | |
84 | ||
85 | r, w, e = popen2.popen3('python slave.py') | |
86 | e.readlines() | |
87 | r.readlines() | |
88 | r.close() | |
89 | e.close() | |
90 | w.close() | |
91 | </pre></div> | |
92 | ||
93 | <P> | |
94 | with code like this in the child: | |
95 | ||
96 | <P> | |
97 | <div class="verbatim"><pre> | |
98 | import os | |
99 | import sys | |
100 | ||
101 | # note that each of these print statements | |
102 | # writes a single long string | |
103 | ||
104 | print >>sys.stderr, 400 * 'this is a test\n' | |
105 | os.close(sys.stderr.fileno()) | |
106 | print >>sys.stdout, 400 * 'this is another test\n' | |
107 | </pre></div> | |
108 | ||
109 | <P> | |
110 | In particular, note that <code>sys.stderr</code> must be closed after | |
111 | writing all data, or <tt class="method">readlines()</tt> won't return. Also note | |
112 | that <tt class="function">os.close()</tt> must be used, as <code>sys.stderr.close()</code> | |
113 | won't close <code>stderr</code> (otherwise assigning to <code>sys.stderr</code> | |
114 | will silently close it, so no further errors can be printed). | |
115 | ||
116 | <P> | |
117 | Applications which need to support a more general approach should | |
118 | integrate I/O over pipes with their <tt class="function">select()</tt> loops, or use | |
119 | separate threads to read each of the individual files provided by | |
120 | whichever <tt class="function">popen*()</tt> function or <tt class="class">Popen*</tt> class was | |
121 | used. | |
122 | ||
123 | <P> | |
124 | ||
125 | <DIV CLASS="navigation"> | |
126 | <div class='online-navigation'> | |
127 | <p></p><hr /> | |
128 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
129 | <tr> | |
130 | <td class='online-navigation'><a rel="prev" title="6.9.1 Popen3 and Popen4" | |
131 | href="popen3-objects.html"><img src='../icons/previous.png' | |
132 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
133 | <td class='online-navigation'><a rel="parent" title="6.9 popen2 " | |
134 | href="module-popen2.html"><img src='../icons/up.png' | |
135 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
136 | <td class='online-navigation'><a rel="next" title="6.10 datetime " | |
137 | href="module-datetime.html"><img src='../icons/next.png' | |
138 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
139 | <td align="center" width="100%">Python Library Reference</td> | |
140 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
141 | href="contents.html"><img src='../icons/contents.png' | |
142 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
143 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
144 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
145 | <td class='online-navigation'><a rel="index" title="Index" | |
146 | href="genindex.html"><img src='../icons/index.png' | |
147 | border='0' height='32' alt='Index' width='32' /></A></td> | |
148 | </tr></table> | |
149 | <div class='online-navigation'> | |
150 | <b class="navlabel">Previous:</b> | |
151 | <a class="sectref" rel="prev" href="popen3-objects.html">6.9.1 Popen3 and Popen4</A> | |
152 | <b class="navlabel">Up:</b> | |
153 | <a class="sectref" rel="parent" href="module-popen2.html">6.9 popen2 </A> | |
154 | <b class="navlabel">Next:</b> | |
155 | <a class="sectref" rel="next" href="module-datetime.html">6.10 datetime </A> | |
156 | </div> | |
157 | </div> | |
158 | <hr /> | |
159 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
160 | </DIV> | |
161 | <!--End of Navigation Panel--> | |
162 | <ADDRESS> | |
163 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
164 | </ADDRESS> | |
165 | </BODY> | |
166 | </HTML> |