Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v9 / html / python / lib / module-asynchat.html
CommitLineData
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="module-asyncore.html" />
13<link rel="parent" href="internet.html" />
14<link rel="next" href="node571.html" />
15<meta name='aesop' content='information' />
16<title>11.26 asynchat -- Asynchronous socket command/response handler</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="11.25.1 asyncore Example basic"
24 href="asyncore-example.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="11. Internet Protocols and"
27 href="internet.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="11.26.1 asynchat - Auxiliary"
30 href="node571.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="asyncore-example.html">11.25.1 asyncore Example basic</A>
45<b class="navlabel">Up:</b>
46<a class="sectref" rel="parent" href="internet.html">11. Internet Protocols and</A>
47<b class="navlabel">Next:</b>
48<a class="sectref" rel="next" href="node571.html">11.26.1 asynchat - Auxiliary</A>
49</div>
50<hr /></div>
51</DIV>
52<!--End of Navigation Panel-->
53
54<H1><A NAME="SECTION00132600000000000000000">
5511.26 <tt class="module">asynchat</tt> --
56 Asynchronous socket command/response handler</A>
57</H1>
58
59<P>
60<A NAME="module-asynchat"></A>
61
62<P>
63This module builds on the <tt class="module"><a href="module-asyncore.html">asyncore</a></tt> infrastructure,
64simplifying asynchronous clients and servers and making it easier to
65handle protocols whose elements are terminated by arbitrary strings, or
66are of variable length. <tt class="module"><a href="module-asynchat.html">asynchat</a></tt> defines the abstract class
67<tt class="class">async_chat</tt> that you subclass, providing implementations of the
68<tt class="method">collect_incoming_data()</tt> and <tt class="method">found_terminator()</tt>
69methods. It uses the same asynchronous loop as <tt class="module"><a href="module-asyncore.html">asyncore</a></tt>, and
70the two types of channel, <tt class="class">asyncore.dispatcher</tt> and
71<tt class="class">asynchat.async_chat</tt>, can freely be mixed in the channel map.
72Typically an <tt class="class">asyncore.dispatcher</tt> server channel generates new
73<tt class="class">asynchat.async_chat</tt> channel objects as it receives incoming
74connection requests.
75
76<P>
77<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
78 <td><nobr><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-3765' xml:id='l2h-3765' class="class">async_chat</tt></b>(</nobr></td>
79 <td><var></var>)</td></tr></table></dt>
80<dd>
81 This class is an abstract subclass of <tt class="class">asyncore.dispatcher</tt>. To make
82 practical use of the code you must subclass <tt class="class">async_chat</tt>, providing
83 meaningful <tt class="method">collect_incoming_data()</tt> and <tt class="method">found_terminator()</tt>
84 methods. The <tt class="class">asyncore.dispatcher</tt> methods can be
85 used, although not all make sense in a message/response context.
86
87<P>
88Like <tt class="class">asyncore.dispatcher</tt>, <tt class="class">async_chat</tt> defines a set of events
89 that are generated by an analysis of socket conditions after a
90 <tt class="cfunction">select()</tt> call. Once the polling loop has been started the
91 <tt class="class">async_chat</tt> object's methods are called by the event-processing
92 framework with no action on the part of the programmer.
93
94<P>
95Unlike <tt class="class">asyncore.dispatcher</tt>, <tt class="class">async_chat</tt> allows you to define
96 a first-in-first-out queue (fifo) of <em>producers</em>. A producer need have
97 only one method, <tt class="method">more()</tt>, which should return data to be transmitted
98 on the channel. The producer indicates exhaustion (<em>i.e.</em> that it contains
99 no more data) by having its <tt class="method">more()</tt> method return the empty string. At
100 this point the <tt class="class">async_chat</tt> object removes the producer from the fifo
101 and starts using the next producer, if any. When the producer fifo is empty
102 the <tt class="method">handle_write()</tt> method does nothing. You use the channel object's
103 <tt class="method">set_terminator()</tt> method to describe how to recognize the end
104 of, or an important breakpoint in, an incoming transmission from the
105 remote endpoint.
106
107<P>
108To build a functioning <tt class="class">async_chat</tt> subclass your
109 input methods <tt class="method">collect_incoming_data()</tt> and
110 <tt class="method">found_terminator()</tt> must handle the data that the channel receives
111 asynchronously. The methods are described below.
112</dl>
113
114<P>
115<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
116 <td><nobr><b><tt id='l2h-3766' xml:id='l2h-3766' class="method">close_when_done</tt></b>(</nobr></td>
117 <td><var></var>)</td></tr></table></dt>
118<dd>
119 Pushes a <code>None</code> on to the producer fifo. When this producer is
120 popped off the fifo it causes the channel to be closed.
121</dl>
122
123<P>
124<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
125 <td><nobr><b><tt id='l2h-3767' xml:id='l2h-3767' class="method">collect_incoming_data</tt></b>(</nobr></td>
126 <td><var>data</var>)</td></tr></table></dt>
127<dd>
128 Called with <var>data</var> holding an arbitrary amount of received data.
129 The default method, which must be overridden, raises a <tt class="exception">NotImplementedError</tt> exception.
130</dl>
131
132<P>
133<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
134 <td><nobr><b><tt id='l2h-3768' xml:id='l2h-3768' class="method">discard_buffers</tt></b>(</nobr></td>
135 <td><var></var>)</td></tr></table></dt>
136<dd>
137 In emergencies this method will discard any data held in the input and/or
138 output buffers and the producer fifo.
139</dl>
140
141<P>
142<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
143 <td><nobr><b><tt id='l2h-3769' xml:id='l2h-3769' class="method">found_terminator</tt></b>(</nobr></td>
144 <td><var></var>)</td></tr></table></dt>
145<dd>
146 Called when the incoming data stream matches the termination condition
147 set by <tt class="method">set_terminator</tt>. The default method, which must be overridden,
148 raises a <tt class="exception">NotImplementedError</tt> exception. The buffered input data should
149 be available via an instance attribute.
150</dl>
151
152<P>
153<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
154 <td><nobr><b><tt id='l2h-3770' xml:id='l2h-3770' class="method">get_terminator</tt></b>(</nobr></td>
155 <td><var></var>)</td></tr></table></dt>
156<dd>
157 Returns the current terminator for the channel.
158</dl>
159
160<P>
161<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
162 <td><nobr><b><tt id='l2h-3771' xml:id='l2h-3771' class="method">handle_close</tt></b>(</nobr></td>
163 <td><var></var>)</td></tr></table></dt>
164<dd>
165 Called when the channel is closed. The default method silently closes
166 the channel's socket.
167</dl>
168
169<P>
170<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
171 <td><nobr><b><tt id='l2h-3772' xml:id='l2h-3772' class="method">handle_read</tt></b>(</nobr></td>
172 <td><var></var>)</td></tr></table></dt>
173<dd>
174 Called when a read event fires on the channel's socket in the
175 asynchronous loop. The default method checks for the termination
176 condition established by <tt class="method">set_terminator()</tt>, which can be either
177 the appearance of a particular string in the input stream or the receipt
178 of a particular number of characters. When the terminator is found,
179 <tt class="method">handle_read</tt> calls the <tt class="method">found_terminator()</tt> method after
180 calling <tt class="method">collect_incoming_data()</tt> with any data preceding the
181 terminating condition.
182</dl>
183
184<P>
185<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
186 <td><nobr><b><tt id='l2h-3773' xml:id='l2h-3773' class="method">handle_write</tt></b>(</nobr></td>
187 <td><var></var>)</td></tr></table></dt>
188<dd>
189 Called when the application may write data to the channel.
190 The default method calls the <tt class="method">initiate_send()</tt> method, which in turn
191 will call <tt class="method">refill_buffer()</tt> to collect data from the producer
192 fifo associated with the channel.
193</dl>
194
195<P>
196<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
197 <td><nobr><b><tt id='l2h-3774' xml:id='l2h-3774' class="method">push</tt></b>(</nobr></td>
198 <td><var>data</var>)</td></tr></table></dt>
199<dd>
200 Creates a <tt class="class">simple_producer</tt> object (<em>see below</em>) containing the data and
201 pushes it on to the channel's <code>producer_fifo</code> to ensure its
202 transmission. This is all you need to do to have the channel write
203 the data out to the network, although it is possible to use your
204 own producers in more complex schemes to implement encryption and
205 chunking, for example.
206</dl>
207
208<P>
209<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
210 <td><nobr><b><tt id='l2h-3775' xml:id='l2h-3775' class="method">push_with_producer</tt></b>(</nobr></td>
211 <td><var>producer</var>)</td></tr></table></dt>
212<dd>
213 Takes a producer object and adds it to the producer fifo associated with
214 the channel. When all currently-pushed producers have been exhausted
215 the channel will consume this producer's data by calling its
216 <tt class="method">more()</tt> method and send the data to the remote endpoint.
217</dl>
218
219<P>
220<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
221 <td><nobr><b><tt id='l2h-3776' xml:id='l2h-3776' class="method">readable</tt></b>(</nobr></td>
222 <td><var></var>)</td></tr></table></dt>
223<dd>
224 Should return <code>True</code> for the channel to be included in the set of
225 channels tested by the <tt class="cfunction">select()</tt> loop for readability.
226</dl>
227
228<P>
229<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
230 <td><nobr><b><tt id='l2h-3777' xml:id='l2h-3777' class="method">refill_buffer</tt></b>(</nobr></td>
231 <td><var></var>)</td></tr></table></dt>
232<dd>
233 Refills the output buffer by calling the <tt class="method">more()</tt> method of the
234 producer at the head of the fifo. If it is exhausted then the
235 producer is popped off the fifo and the next producer is activated.
236 If the current producer is, or becomes, <code>None</code> then the channel
237 is closed.
238</dl>
239
240<P>
241<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
242 <td><nobr><b><tt id='l2h-3778' xml:id='l2h-3778' class="method">set_terminator</tt></b>(</nobr></td>
243 <td><var>term</var>)</td></tr></table></dt>
244<dd>
245 Sets the terminating condition to be recognised on the channel. <code>term</code>
246 may be any of three types of value, corresponding to three different ways
247 to handle incoming protocol data.
248
249<P>
250<div class="center"><table class="realtable">
251 <thead>
252 <tr>
253 <th class="left" >term</th>
254 <th class="left" >Description</th>
255 </tr>
256 </thead>
257 <tbody>
258 <tr><td class="left" valign="baseline"><em>string</em></td>
259 <td class="left" >Will call <tt class="method">found_terminator()</tt> when the
260 string is found in the input stream</td></tr>
261 <tr><td class="left" valign="baseline"><em>integer</em></td>
262 <td class="left" >Will call <tt class="method">found_terminator()</tt> when the
263 indicated number of characters have been received</td></tr>
264 <tr><td class="left" valign="baseline"><code>None</code></td>
265 <td class="left" >The channel continues to collect data forever</td></tr></tbody>
266</table></div>
267
268<P>
269Note that any data following the terminator will be available for reading by
270 the channel after <tt class="method">found_terminator()</tt> is called.
271</dl>
272
273<P>
274<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
275 <td><nobr><b><tt id='l2h-3779' xml:id='l2h-3779' class="method">writable</tt></b>(</nobr></td>
276 <td><var></var>)</td></tr></table></dt>
277<dd>
278 Should return <code>True</code> as long as items remain on the producer fifo,
279 or the channel is connected and the channel's output buffer is non-empty.
280</dl>
281
282<P>
283
284<p><br /></p><hr class='online-navigation' />
285<div class='online-navigation'>
286<!--Table of Child-Links-->
287<A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a>
288
289<UL CLASS="ChildLinks">
290<LI><A href="node571.html">11.26.1 asynchat - Auxiliary Classes and Functions</a>
291<LI><A href="asynchat-example.html">11.26.2 asynchat Example</a>
292</ul>
293<!--End of Table of Child-Links-->
294</div>
295
296<DIV CLASS="navigation">
297<div class='online-navigation'>
298<p></p><hr />
299<table align="center" width="100%" cellpadding="0" cellspacing="2">
300<tr>
301<td class='online-navigation'><a rel="prev" title="11.25.1 asyncore Example basic"
302 href="asyncore-example.html"><img src='../icons/previous.png'
303 border='0' height='32' alt='Previous Page' width='32' /></A></td>
304<td class='online-navigation'><a rel="parent" title="11. Internet Protocols and"
305 href="internet.html"><img src='../icons/up.png'
306 border='0' height='32' alt='Up One Level' width='32' /></A></td>
307<td class='online-navigation'><a rel="next" title="11.26.1 asynchat - Auxiliary"
308 href="node571.html"><img src='../icons/next.png'
309 border='0' height='32' alt='Next Page' width='32' /></A></td>
310<td align="center" width="100%">Python Library Reference</td>
311<td class='online-navigation'><a rel="contents" title="Table of Contents"
312 href="contents.html"><img src='../icons/contents.png'
313 border='0' height='32' alt='Contents' width='32' /></A></td>
314<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
315 border='0' height='32' alt='Module Index' width='32' /></a></td>
316<td class='online-navigation'><a rel="index" title="Index"
317 href="genindex.html"><img src='../icons/index.png'
318 border='0' height='32' alt='Index' width='32' /></A></td>
319</tr></table>
320<div class='online-navigation'>
321<b class="navlabel">Previous:</b>
322<a class="sectref" rel="prev" href="asyncore-example.html">11.25.1 asyncore Example basic</A>
323<b class="navlabel">Up:</b>
324<a class="sectref" rel="parent" href="internet.html">11. Internet Protocols and</A>
325<b class="navlabel">Next:</b>
326<a class="sectref" rel="next" href="node571.html">11.26.1 asynchat - Auxiliary</A>
327</div>
328</div>
329<hr />
330<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
331</DIV>
332<!--End of Navigation Panel-->
333<ADDRESS>
334See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
335</ADDRESS>
336</BODY>
337</HTML>