Update to 2.7.3
[profile/ivi/python.git] / Doc / library / asyncore.rst
1 :mod:`asyncore` --- Asynchronous socket handler
2 ===============================================
3
4 .. module:: asyncore
5    :synopsis: A base class for developing asynchronous socket handling
6               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
11
12 **Source code:** :source:`Lib/asyncore.py`
13
14 --------------
15
16 This module provides the basic infrastructure for writing asynchronous  socket
17 service clients and servers.
18
19 There are only two ways to have a program on a single processor do  "more than
20 one thing at a time." Multi-threaded programming is the  simplest and most
21 popular way to do it, but there is another very different technique, that lets
22 you have nearly all the advantages of  multi-threading, without actually using
23 multiple threads.  It's really  only practical if your program is largely I/O
24 bound.  If your program is processor bound, then pre-emptive scheduled threads
25 are probably what you really need.  Network servers are rarely processor
26 bound, however.
27
28 If your operating system supports the :c:func:`select` system call in its I/O
29 library (and nearly all do), then you can use it to juggle multiple
30 communication channels at once; doing other work while your I/O is taking
31 place in the "background."  Although this strategy can seem strange and
32 complex, especially at first, it is in many ways easier to understand and
33 control than multi-threaded programming.  The :mod:`asyncore` module solves
34 many of the difficult problems for you, making the task of building
35 sophisticated high-performance network servers and clients a snap.  For
36 "conversational" applications and protocols the companion :mod:`asynchat`
37 module is invaluable.
38
39 The basic idea behind both modules is to create one or more network
40 *channels*, instances of class :class:`asyncore.dispatcher` and
41 :class:`asynchat.async_chat`.  Creating the channels adds them to a global
42 map, used by the :func:`loop` function if you do not provide it with your own
43 *map*.
44
45 Once the initial channel(s) is(are) created, calling the :func:`loop` function
46 activates channel service, which continues until the last channel (including
47 any that have been added to the map during asynchronous service) is closed.
48
49
50 .. function:: loop([timeout[, use_poll[, map[,count]]]])
51
52    Enter a polling loop that terminates after count passes or all open
53    channels have been closed.  All arguments are optional.  The *count*
54    parameter defaults to None, resulting in the loop terminating only when all
55    channels have been closed.  The *timeout* argument sets the timeout
56    parameter for the appropriate :func:`select` or :func:`poll` call, measured
57    in seconds; the default is 30 seconds.  The *use_poll* parameter, if true,
58    indicates that :func:`poll` should be used in preference to :func:`select`
59    (the default is ``False``).
60
61    The *map* parameter is a dictionary whose items are the channels to watch.
62    As channels are closed they are deleted from their map.  If *map* is
63    omitted, a global map is used. Channels (instances of
64    :class:`asyncore.dispatcher`, :class:`asynchat.async_chat` and subclasses
65    thereof) can freely be mixed in the map.
66
67
68 .. class:: dispatcher()
69
70    The :class:`dispatcher` class is a thin wrapper around a low-level socket
71    object. To make it more useful, it has a few methods for event-handling
72    which are called from the asynchronous loop.   Otherwise, it can be treated
73    as a normal non-blocking socket object.
74
75    The firing of low-level events at certain times or in certain connection
76    states tells the asynchronous loop that certain higher-level events have
77    taken place.  For example, if we have asked for a socket to connect to
78    another host, we know that the connection has been made when the socket
79    becomes writable for the first time (at this point you know that you may
80    write to it with the expectation of success).  The implied higher-level
81    events are:
82
83    +----------------------+----------------------------------------+
84    | Event                | Description                            |
85    +======================+========================================+
86    | ``handle_connect()`` | Implied by the first read or write     |
87    |                      | event                                  |
88    +----------------------+----------------------------------------+
89    | ``handle_close()``   | Implied by a read event with no data   |
90    |                      | available                              |
91    +----------------------+----------------------------------------+
92    | ``handle_accept()``  | Implied by a read event on a listening |
93    |                      | socket                                 |
94    +----------------------+----------------------------------------+
95
96    During asynchronous processing, each mapped channel's :meth:`readable` and
97    :meth:`writable` methods are used to determine whether the channel's socket
98    should be added to the list of channels :c:func:`select`\ ed or
99    :c:func:`poll`\ ed for read and write events.
100
101    Thus, the set of channel events is larger than the basic socket events.  The
102    full set of methods that can be overridden in your subclass follows:
103
104
105    .. method:: handle_read()
106
107       Called when the asynchronous loop detects that a :meth:`read` call on the
108       channel's socket will succeed.
109
110
111    .. method:: handle_write()
112
113       Called when the asynchronous loop detects that a writable socket can be
114       written.  Often this method will implement the necessary buffering for
115       performance.  For example::
116
117          def handle_write(self):
118              sent = self.send(self.buffer)
119              self.buffer = self.buffer[sent:]
120
121
122    .. method:: handle_expt()
123
124       Called when there is out of band (OOB) data for a socket connection.  This
125       will almost never happen, as OOB is tenuously supported and rarely used.
126
127
128    .. method:: handle_connect()
129
130       Called when the active opener's socket actually makes a connection.  Might
131       send a "welcome" banner, or initiate a protocol negotiation with the
132       remote endpoint, for example.
133
134
135    .. method:: handle_close()
136
137       Called when the socket is closed.
138
139
140    .. method:: handle_error()
141
142       Called when an exception is raised and not otherwise handled.  The default
143       version prints a condensed traceback.
144
145
146    .. method:: handle_accept()
147
148       Called on listening channels (passive openers) when a connection can be
149       established with a new remote endpoint that has issued a :meth:`connect`
150       call for the local endpoint.
151
152
153    .. method:: readable()
154
155       Called each time around the asynchronous loop to determine whether a
156       channel's socket should be added to the list on which read events can
157       occur.  The default method simply returns ``True``, indicating that by
158       default, all channels will be interested in read events.
159
160
161    .. method:: writable()
162
163       Called each time around the asynchronous loop to determine whether a
164       channel's socket should be added to the list on which write events can
165       occur.  The default method simply returns ``True``, indicating that by
166       default, all channels will be interested in write events.
167
168
169    In addition, each channel delegates or extends many of the socket methods.
170    Most of these are nearly identical to their socket partners.
171
172
173    .. method:: create_socket(family, type)
174
175       This is identical to the creation of a normal socket, and will use the
176       same options for creation.  Refer to the :mod:`socket` documentation for
177       information on creating sockets.
178
179
180    .. method:: connect(address)
181
182       As with the normal socket object, *address* is a tuple with the first
183       element the host to connect to, and the second the port number.
184
185
186    .. method:: send(data)
187
188       Send *data* to the remote end-point of the socket.
189
190
191    .. method:: recv(buffer_size)
192
193       Read at most *buffer_size* bytes from the socket's remote end-point.  An
194       empty string implies that the channel has been closed from the other end.
195
196
197    .. method:: listen(backlog)
198
199       Listen for connections made to the socket.  The *backlog* argument
200       specifies the maximum number of queued connections and should be at least
201       1; the maximum value is system-dependent (usually 5).
202
203
204    .. method:: bind(address)
205
206       Bind the socket to *address*.  The socket must not already be bound.  (The
207       format of *address* depends on the address family --- refer to the
208       :mod:`socket` documentation for more information.)  To mark
209       the socket as re-usable (setting the :const:`SO_REUSEADDR` option), call
210       the :class:`dispatcher` object's :meth:`set_reuse_addr` method.
211
212
213    .. method:: accept()
214
215       Accept a connection.  The socket must be bound to an address and listening
216       for connections.  The return value can be either ``None`` or a pair
217       ``(conn, address)`` where *conn* is a *new* socket object usable to send
218       and receive data on the connection, and *address* is the address bound to
219       the socket on the other end of the connection.
220       When ``None`` is returned it means the connection didn't take place, in
221       which case the server should just ignore this event and keep listening
222       for further incoming connections.
223
224
225    .. method:: close()
226
227       Close the socket.  All future operations on the socket object will fail.
228       The remote end-point will receive no more data (after queued data is
229       flushed).  Sockets are automatically closed when they are
230       garbage-collected.
231
232 .. class:: dispatcher_with_send()
233
234    A :class:`dispatcher` subclass which adds simple buffered output capability,
235    useful for simple clients. For more sophisticated usage use
236    :class:`asynchat.async_chat`.
237
238 .. class:: file_dispatcher()
239
240    A file_dispatcher takes a file descriptor or file object along with an
241    optional map argument and wraps it for use with the :c:func:`poll` or
242    :c:func:`loop` functions.  If provided a file object or anything with a
243    :c:func:`fileno` method, that method will be called and passed to the
244    :class:`file_wrapper` constructor.  Availability: UNIX.
245
246 .. class:: file_wrapper()
247
248    A file_wrapper takes an integer file descriptor and calls :func:`os.dup` to
249    duplicate the handle so that the original handle may be closed independently
250    of the file_wrapper.  This class implements sufficient methods to emulate a
251    socket for use by the :class:`file_dispatcher` class.  Availability: UNIX.
252
253
254 .. _asyncore-example-1:
255
256 asyncore Example basic HTTP client
257 ----------------------------------
258
259 Here is a very basic HTTP client that uses the :class:`dispatcher` class to
260 implement its socket handling::
261
262    import asyncore, socket
263
264    class HTTPClient(asyncore.dispatcher):
265
266        def __init__(self, host, path):
267            asyncore.dispatcher.__init__(self)
268            self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
269            self.connect( (host, 80) )
270            self.buffer = 'GET %s HTTP/1.0\r\n\r\n' % path
271
272        def handle_connect(self):
273            pass
274
275        def handle_close(self):
276            self.close()
277
278        def handle_read(self):
279            print self.recv(8192)
280
281        def writable(self):
282            return (len(self.buffer) > 0)
283
284        def handle_write(self):
285            sent = self.send(self.buffer)
286            self.buffer = self.buffer[sent:]
287
288
289    client = HTTPClient('www.python.org', '/')
290    asyncore.loop()
291
292 .. _asyncore-example-2:
293
294 asyncore Example basic echo server
295 ----------------------------------
296
297 Here is a basic echo server that uses the :class:`dispatcher` class to accept
298 connections and dispatches the incoming connections to a handler::
299
300     import asyncore
301     import socket
302
303     class EchoHandler(asyncore.dispatcher_with_send):
304
305         def handle_read(self):
306             data = self.recv(8192)
307             if data:
308                 self.send(data)
309
310     class EchoServer(asyncore.dispatcher):
311
312         def __init__(self, host, port):
313             asyncore.dispatcher.__init__(self)
314             self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
315             self.set_reuse_addr()
316             self.bind((host, port))
317             self.listen(5)
318
319         def handle_accept(self):
320             pair = self.accept()
321             if pair is None:
322                 pass
323             else:
324                 sock, addr = pair
325                 print 'Incoming connection from %s' % repr(addr)
326                 handler = EchoHandler(sock)
327
328     server = EchoServer('localhost', 8080)
329     asyncore.loop()
330