AddressList.__str__(): Get rid of useless, and broken method. Closes
[python/dscho.git] / Doc / lib / libasyncore.tex
blob89c717810bcaa90e4d0e1a276ebb968833166918
1 \section{\module{asyncore} ---
2 Asynchronous socket handler}
4 \declaremodule{builtin}{asyncore}
5 \modulesynopsis{A base class for developing asynchronous socket
6 handling services.}
7 \moduleauthor{Sam Rushing}{rushing@nightmare.com}
8 \sectionauthor{Christopher Petrilli}{petrilli@amber.org}
9 \sectionauthor{Steve Holden}{sholden@holdenweb.com}
10 % Heavily adapted from original documentation by Sam Rushing.
12 This module provides the basic infrastructure for writing asynchronous
13 socket service clients and servers.
15 There are only two ways to have a program on a single processor do
16 ``more than one thing at a time.'' Multi-threaded programming is the
17 simplest and most popular way to do it, but there is another very
18 different technique, that lets you have nearly all the advantages of
19 multi-threading, without actually using multiple threads. It's really
20 only practical if your program is largely I/O bound. If your program
21 is processor bound, then pre-emptive scheduled threads are probably what
22 you really need. Network servers are rarely processor bound, however.
24 If your operating system supports the \cfunction{select()} system call
25 in its I/O library (and nearly all do), then you can use it to juggle
26 multiple communication channels at once; doing other work while your
27 I/O is taking place in the ``background.'' Although this strategy can
28 seem strange and complex, especially at first, it is in many ways
29 easier to understand and control than multi-threaded programming.
30 The \module{asyncore} module solves many of the difficult problems for
31 you, making the task of building sophisticated high-performance
32 network servers and clients a snap. For ``conversational'' applications
33 and protocols the companion \refmodule{asynchat} module is invaluable.
35 The basic idea behind both modules is to create one or more network
36 \emph{channels}, instances of class \class{asyncore.dispatcher} and
37 \class{asynchat.async_chat}. Creating the channels adds them to a global
38 map, used by the \function{loop()} function if you do not provide it
39 with your own \var{map}.
41 Once the initial channel(s) is(are) created, calling the \function{loop()}
42 function activates channel service, which continues until the last
43 channel (including any that have been added to the map during asynchronous
44 service) is closed.
46 \begin{funcdesc}{loop}{\optional{timeout\optional{, use_poll\optional{,
47 map}}}}
48 Enter a polling loop that only terminates after all open channels
49 have been closed. All arguments are optional. The \var{timeout}
50 argument sets the timeout parameter for the appropriate
51 \function{select()} or \function{poll()} call, measured in seconds;
52 the default is 30 seconds. The \var{use_poll} parameter, if true,
53 indicates that \function{poll()} should be used in preference to
54 \function{select()} (the default is \code{False}). The \var{map} parameter
55 is a dictionary whose items are the channels to watch. As channels
56 are closed they are deleted from their map. If \var{map} is
57 omitted, a global map is used (this map is updated by the default
58 class \method{__init__()}
59 -- make sure you extend, rather than override, \method{__init__()}
60 if you want to retain this behavior).
62 Channels (instances of \class{asyncore.dispatcher}, \class{asynchat.async_chat}
63 and subclasses thereof) can freely be mixed in the map.
64 \end{funcdesc}
66 \begin{classdesc}{dispatcher}{}
67 The \class{dispatcher} class is a thin wrapper around a low-level socket object.
68 To make it more useful, it has a few methods for event-handling which are called
69 from the asynchronous loop.
70 Otherwise, it can be treated as a normal non-blocking socket object.
72 Two class attributes can be modified, to improve performance,
73 or possibly even to conserve memory.
75 \begin{datadesc}{ac_in_buffer_size}
76 The asynchronous input buffer size (default \code{4096}).
77 \end{datadesc}
79 \begin{datadesc}{ac_out_buffer_size}
80 The asynchronous output buffer size (default \code{4096}).
81 \end{datadesc}
83 The firing of low-level events at certain times or in certain connection
84 states tells the asynchronous loop that certain higher-level events have
85 taken place. For example, if we have asked for a socket to connect to
86 another host, we know that the connection has been made when the socket
87 becomes writable for the first time (at this point you know that you may
88 write to it with the expectation of success). The implied higher-level
89 events are:
91 \begin{tableii}{l|l}{code}{Event}{Description}
92 \lineii{handle_connect()}{Implied by the first write event}
93 \lineii{handle_close()}{Implied by a read event with no data available}
94 \lineii{handle_accept()}{Implied by a read event on a listening socket}
95 \end{tableii}
97 During asynchronous processing, each mapped channel's \method{readable()}
98 and \method{writable()} methods are used to determine whether the channel's
99 socket should be added to the list of channels \cfunction{select()}ed or
100 \cfunction{poll()}ed for read and write events.
102 \end{classdesc}
104 Thus, the set of channel events is larger than the basic socket events.
105 The full set of methods that can be overridden in your subclass follows:
107 \begin{methoddesc}{handle_read}{}
108 Called when the asynchronous loop detects that a \method{read()}
109 call on the channel's socket will succeed.
110 \end{methoddesc}
112 \begin{methoddesc}{handle_write}{}
113 Called when the asynchronous loop detects that a writable socket
114 can be written.
115 Often this method will implement the necessary buffering for
116 performance. For example:
118 \begin{verbatim}
119 def handle_write(self):
120 sent = self.send(self.buffer)
121 self.buffer = self.buffer[sent:]
122 \end{verbatim}
123 \end{methoddesc}
125 \begin{methoddesc}{handle_expt}{}
126 Called when there is out of band (OOB) data for a socket
127 connection. This will almost never happen, as OOB is
128 tenuously supported and rarely used.
129 \end{methoddesc}
131 \begin{methoddesc}{handle_connect}{}
132 Called when the active opener's socket actually makes a connection.
133 Might send a ``welcome'' banner, or initiate a protocol
134 negotiation with the remote endpoint, for example.
135 \end{methoddesc}
137 \begin{methoddesc}{handle_close}{}
138 Called when the socket is closed.
139 \end{methoddesc}
141 \begin{methoddesc}{handle_error}{}
142 Called when an exception is raised and not otherwise handled. The default
143 version prints a condensed traceback.
144 \end{methoddesc}
146 \begin{methoddesc}{handle_accept}{}
147 Called on listening channels (passive openers) when a
148 connection can be established with a new remote endpoint that
149 has issued a \method{connect()} call for the local endpoint.
150 \end{methoddesc}
152 \begin{methoddesc}{readable}{}
153 Called each time around the asynchronous loop to determine whether a
154 channel's socket should be added to the list on which read events can
155 occur. The default method simply returns \code{True},
156 indicating that by default, all channels will be interested in
157 read events.
158 \end{methoddesc}
160 \begin{methoddesc}{writable}{}
161 Called each time around the asynchronous loop to determine whether a
162 channel's socket should be added to the list on which write events can
163 occur. The default method simply returns \code{True},
164 indicating that by default, all channels will be interested in
165 write events.
166 \end{methoddesc}
168 In addition, each channel delegates or extends many of the socket methods.
169 Most of these are nearly identical to their socket partners.
171 \begin{methoddesc}{create_socket}{family, type}
172 This is identical to the creation of a normal socket, and
173 will use the same options for creation. Refer to the
174 \refmodule{socket} documentation for information on creating
175 sockets.
176 \end{methoddesc}
178 \begin{methoddesc}{connect}{address}
179 As with the normal socket object, \var{address} is a
180 tuple with the first element the host to connect to, and the
181 second the port number.
182 \end{methoddesc}
184 \begin{methoddesc}{send}{data}
185 Send \var{data} to the remote end-point of the socket.
186 \end{methoddesc}
188 \begin{methoddesc}{recv}{buffer_size}
189 Read at most \var{buffer_size} bytes from the socket's remote end-point.
190 An empty string implies that the channel has been closed from the other
191 end.
192 \end{methoddesc}
194 \begin{methoddesc}{listen}{backlog}
195 Listen for connections made to the socket. The \var{backlog}
196 argument specifies the maximum number of queued connections
197 and should be at least 1; the maximum value is
198 system-dependent (usually 5).
199 \end{methoddesc}
201 \begin{methoddesc}{bind}{address}
202 Bind the socket to \var{address}. The socket must not already
203 be bound. (The format of \var{address} depends on the address
204 family --- see above.)
205 \end{methoddesc}
207 \begin{methoddesc}{accept}{}
208 Accept a connection. The socket must be bound to an address
209 and listening for connections. The return value is a pair
210 \code{(\var{conn}, \var{address})} where \var{conn} is a
211 \emph{new} socket object usable to send and receive data on
212 the connection, and \var{address} is the address bound to the
213 socket on the other end of the connection.
214 \end{methoddesc}
216 \begin{methoddesc}{close}{}
217 Close the socket. All future operations on the socket object
218 will fail. The remote end-point will receive no more data (after
219 queued data is flushed). Sockets are automatically closed
220 when they are garbage-collected.
221 \end{methoddesc}
224 \subsection{asyncore Example basic HTTP client \label{asyncore-example}}
226 As a basic example, below is a very basic HTTP client that uses the
227 \class{dispatcher} class to implement its socket handling:
229 \begin{verbatim}
230 class http_client(asyncore.dispatcher):
231 def __init__(self, host,path):
232 asyncore.dispatcher.__init__(self)
233 self.path = path
234 self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
235 self.connect( (host, 80) )
236 self.buffer = 'GET %s HTTP/1.0\r\n\r\n' % self.path
238 def handle_connect(self):
239 pass
241 def handle_read(self):
242 data = self.recv(8192)
243 print data
245 def writable(self):
246 return (len(self.buffer) > 0)
248 def handle_write(self):
249 sent = self.send(self.buffer)
250 self.buffer = self.buffer[sent:]
251 \end{verbatim}