/Doc/library/asyncore.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 278 lines · 187 code · 91 blank · 0 comment · 0 complexity · 95651df0f8b9f3af175d8135b51a1364 MD5 · raw file

  1. :mod:`asyncore` --- Asynchronous socket handler
  2. ===============================================
  3. .. module:: asyncore
  4. :synopsis: A base class for developing asynchronous socket handling
  5. services.
  6. .. moduleauthor:: Sam Rushing <rushing@nightmare.com>
  7. .. sectionauthor:: Christopher Petrilli <petrilli@amber.org>
  8. .. sectionauthor:: Steve Holden <sholden@holdenweb.com>
  9. .. heavily adapted from original documentation by Sam Rushing
  10. This module provides the basic infrastructure for writing asynchronous socket
  11. service clients and servers.
  12. There are only two ways to have a program on a single processor do "more than
  13. one thing at a time." Multi-threaded programming is the simplest and most
  14. popular way to do it, but there is another very different technique, that lets
  15. you have nearly all the advantages of multi-threading, without actually using
  16. multiple threads. It's really only practical if your program is largely I/O
  17. bound. If your program is processor bound, then pre-emptive scheduled threads
  18. are probably what you really need. Network servers are rarely processor
  19. bound, however.
  20. If your operating system supports the :cfunc:`select` system call in its I/O
  21. library (and nearly all do), then you can use it to juggle multiple
  22. communication channels at once; doing other work while your I/O is taking
  23. place in the "background." Although this strategy can seem strange and
  24. complex, especially at first, it is in many ways easier to understand and
  25. control than multi-threaded programming. The :mod:`asyncore` module solves
  26. many of the difficult problems for you, making the task of building
  27. sophisticated high-performance network servers and clients a snap. For
  28. "conversational" applications and protocols the companion :mod:`asynchat`
  29. module is invaluable.
  30. The basic idea behind both modules is to create one or more network
  31. *channels*, instances of class :class:`asyncore.dispatcher` and
  32. :class:`asynchat.async_chat`. Creating the channels adds them to a global
  33. map, used by the :func:`loop` function if you do not provide it with your own
  34. *map*.
  35. Once the initial channel(s) is(are) created, calling the :func:`loop` function
  36. activates channel service, which continues until the last channel (including
  37. any that have been added to the map during asynchronous service) is closed.
  38. .. function:: loop([timeout[, use_poll[, map[,count]]]])
  39. Enter a polling loop that terminates after count passes or all open
  40. channels have been closed. All arguments are optional. The *count*
  41. parameter defaults to None, resulting in the loop terminating only when all
  42. channels have been closed. The *timeout* argument sets the timeout
  43. parameter for the appropriate :func:`select` or :func:`poll` call, measured
  44. in seconds; the default is 30 seconds. The *use_poll* parameter, if true,
  45. indicates that :func:`poll` should be used in preference to :func:`select`
  46. (the default is ``False``).
  47. The *map* parameter is a dictionary whose items are the channels to watch.
  48. As channels are closed they are deleted from their map. If *map* is
  49. omitted, a global map is used. Channels (instances of
  50. :class:`asyncore.dispatcher`, :class:`asynchat.async_chat` and subclasses
  51. thereof) can freely be mixed in the map.
  52. .. class:: dispatcher()
  53. The :class:`dispatcher` class is a thin wrapper around a low-level socket
  54. object. To make it more useful, it has a few methods for event-handling
  55. which are called from the asynchronous loop. Otherwise, it can be treated
  56. as a normal non-blocking socket object.
  57. The firing of low-level events at certain times or in certain connection
  58. states tells the asynchronous loop that certain higher-level events have
  59. taken place. For example, if we have asked for a socket to connect to
  60. another host, we know that the connection has been made when the socket
  61. becomes writable for the first time (at this point you know that you may
  62. write to it with the expectation of success). The implied higher-level
  63. events are:
  64. +----------------------+----------------------------------------+
  65. | Event | Description |
  66. +======================+========================================+
  67. | ``handle_connect()`` | Implied by the first read or write |
  68. | | event |
  69. +----------------------+----------------------------------------+
  70. | ``handle_close()`` | Implied by a read event with no data |
  71. | | available |
  72. +----------------------+----------------------------------------+
  73. | ``handle_accept()`` | Implied by a read event on a listening |
  74. | | socket |
  75. +----------------------+----------------------------------------+
  76. During asynchronous processing, each mapped channel's :meth:`readable` and
  77. :meth:`writable` methods are used to determine whether the channel's socket
  78. should be added to the list of channels :cfunc:`select`\ ed or
  79. :cfunc:`poll`\ ed for read and write events.
  80. Thus, the set of channel events is larger than the basic socket events. The
  81. full set of methods that can be overridden in your subclass follows:
  82. .. method:: handle_read()
  83. Called when the asynchronous loop detects that a :meth:`read` call on the
  84. channel's socket will succeed.
  85. .. method:: handle_write()
  86. Called when the asynchronous loop detects that a writable socket can be
  87. written. Often this method will implement the necessary buffering for
  88. performance. For example::
  89. def handle_write(self):
  90. sent = self.send(self.buffer)
  91. self.buffer = self.buffer[sent:]
  92. .. method:: handle_expt()
  93. Called when there is out of band (OOB) data for a socket connection. This
  94. will almost never happen, as OOB is tenuously supported and rarely used.
  95. .. method:: handle_connect()
  96. Called when the active opener's socket actually makes a connection. Might
  97. send a "welcome" banner, or initiate a protocol negotiation with the
  98. remote endpoint, for example.
  99. .. method:: handle_close()
  100. Called when the socket is closed.
  101. .. method:: handle_error()
  102. Called when an exception is raised and not otherwise handled. The default
  103. version prints a condensed traceback.
  104. .. method:: handle_accept()
  105. Called on listening channels (passive openers) when a connection can be
  106. established with a new remote endpoint that has issued a :meth:`connect`
  107. call for the local endpoint.
  108. .. method:: readable()
  109. Called each time around the asynchronous loop to determine whether a
  110. channel's socket should be added to the list on which read events can
  111. occur. The default method simply returns ``True``, indicating that by
  112. default, all channels will be interested in read events.
  113. .. method:: writable()
  114. Called each time around the asynchronous loop to determine whether a
  115. channel's socket should be added to the list on which write events can
  116. occur. The default method simply returns ``True``, indicating that by
  117. default, all channels will be interested in write events.
  118. In addition, each channel delegates or extends many of the socket methods.
  119. Most of these are nearly identical to their socket partners.
  120. .. method:: create_socket(family, type)
  121. This is identical to the creation of a normal socket, and will use the
  122. same options for creation. Refer to the :mod:`socket` documentation for
  123. information on creating sockets.
  124. .. method:: connect(address)
  125. As with the normal socket object, *address* is a tuple with the first
  126. element the host to connect to, and the second the port number.
  127. .. method:: send(data)
  128. Send *data* to the remote end-point of the socket.
  129. .. method:: recv(buffer_size)
  130. Read at most *buffer_size* bytes from the socket's remote end-point. An
  131. empty string implies that the channel has been closed from the other end.
  132. .. method:: listen(backlog)
  133. Listen for connections made to the socket. The *backlog* argument
  134. specifies the maximum number of queued connections and should be at least
  135. 1; the maximum value is system-dependent (usually 5).
  136. .. method:: bind(address)
  137. Bind the socket to *address*. The socket must not already be bound. (The
  138. format of *address* depends on the address family --- see above.) To mark
  139. the socket as re-usable (setting the :const:`SO_REUSEADDR` option), call
  140. the :class:`dispatcher` object's :meth:`set_reuse_addr` method.
  141. .. method:: accept()
  142. Accept a connection. The socket must be bound to an address and listening
  143. for connections. The return value is a pair ``(conn, address)`` where
  144. *conn* is a *new* socket object usable to send and receive data on the
  145. connection, and *address* is the address bound to the socket on the other
  146. end of the connection.
  147. .. method:: close()
  148. Close the socket. All future operations on the socket object will fail.
  149. The remote end-point will receive no more data (after queued data is
  150. flushed). Sockets are automatically closed when they are
  151. garbage-collected.
  152. .. class:: file_dispatcher()
  153. A file_dispatcher takes a file descriptor or file object along with an
  154. optional map argument and wraps it for use with the :cfunc:`poll` or
  155. :cfunc:`loop` functions. If provided a file object or anything with a
  156. :cfunc:`fileno` method, that method will be called and passed to the
  157. :class:`file_wrapper` constructor. Availability: UNIX.
  158. .. class:: file_wrapper()
  159. A file_wrapper takes an integer file descriptor and calls :func:`os.dup` to
  160. duplicate the handle so that the original handle may be closed independently
  161. of the file_wrapper. This class implements sufficient methods to emulate a
  162. socket for use by the :class:`file_dispatcher` class. Availability: UNIX.
  163. .. _asyncore-example:
  164. asyncore Example basic HTTP client
  165. ----------------------------------
  166. Here is a very basic HTTP client that uses the :class:`dispatcher` class to
  167. implement its socket handling::
  168. import asyncore, socket
  169. class http_client(asyncore.dispatcher):
  170. def __init__(self, host, path):
  171. asyncore.dispatcher.__init__(self)
  172. self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
  173. self.connect( (host, 80) )
  174. self.buffer = 'GET %s HTTP/1.0\r\n\r\n' % path
  175. def handle_connect(self):
  176. pass
  177. def handle_close(self):
  178. self.close()
  179. def handle_read(self):
  180. print self.recv(8192)
  181. def writable(self):
  182. return (len(self.buffer) > 0)
  183. def handle_write(self):
  184. sent = self.send(self.buffer)
  185. self.buffer = self.buffer[sent:]
  186. c = http_client('www.python.org', '/')
  187. asyncore.loop()